views:

43

answers:

3

Hello,

I have this code,

$(function () {
    $.post("fetch_data.php?url="+$('#url').val(), { }, function(response){
        var arrValues = [ <?php echo $js_img_arr; ?> ]; //output from php

        $.each(arrValues, function( intIndex, objValue ) {
        var img = new Image(); //creating four Image Elements
            img.src = objValue;
            img.onload = function() {
            if(img.height > 100 && img.width > 200) {
                alert('height: ' + img.height + ' width: ' + img.width);
                //here
            }
            }
        });
    });
});

I am new to jquery/javascript.

Is it possible to get value of how many images loaded at last, let say 10 img have loaded and met the height/width criteria, i want the value of 10 images loaded.

Thank you.

A: 

Hi,

I dont think var arrValues = [ <?php echo $js_img_arr; ?> ]; will work as it is inside the callback function of the $.post method. the code inside

function(response){
  ....
}

will execute only after the ajax request is completed and a response is available which can be accessed using the 'response' parameter. you can decide in what form you want this response (xml, json , html, text etc) and then create the array arrValues accordingly.

What is your response btw. what are you echo-ing in the fetch_data.php file ?

naiquevin
its working, i just need to get last value of each loop..
cicakman
that will work. learn about closures
Andrew Bullock
oh yes.. thanks
naiquevin
A: 

You should use a variable to count.

$(function () {
   $.post("fetch_data.php?url="+$('#url').val(), { }, function(response){
       var arrValues = [ <?php echo $js_img_arr; ?> ]; //output from php
       var imgCount  = 0;

       $.each(arrValues, function( intIndex, objValue ) {
       var img = new Image(); //creating four Image Elements
           img.src = objValue;
           img.onload = function() {
              if(img.height > 100 && img.width > 200) {
                //alert('height: ' + img.height + ' width: ' + img.width);
                if(imgCount % 10 === 0){
                  // 10th image loaded
                }

                imgCount++;
              }
           }
       });
   });
});
jAndy
well, i was just giving example of 10, how do i get last value on hereif(imgCount % 10 === 0)
cicakman
+1  A: 
$(function () {
    $.post("fetch_data.php?url="+$('#url').val(), { }, function(response){
        var arrValues = [ <?php echo $js_img_arr; ?> ]; //output from php
        var length = 0; //counter variable 

        $.each(arrValues, function( intIndex, objValue ) {
        var img = new Image(); //creating four Image Elements
            img.src = objValue;
            img.onload = function() {
               length++;   //Increment the counter  by One
               if(img.height > 100 && img.width > 200) {
                   alert('height: ' + img.height + ' width: ' + img.width);
               }
            }

            alert('Number of Images loaded : '+ length); //Total number of Images
        });
    });
Ninja Dude
@cicakman I'm not sure. Is it the result you want ?
Ninja Dude