0
var J_lis = $("#Jerseybox ul li img");
var count_jersey = 0
J_lis.hide();
J_lis.each(function(g) {
    $(this).delay((g + 1) * 360).fadeIn();
    count_jersey++;
});

i am using $.each to make it fadeIn image one by one , using this code ,but is there anyway i could know last image fadeIn ?

my intetion is to make those image fadeIn one by one and when the last the image finished fadeIn i will remove something.

how do i check last Image finished fadeIn?

darkness
  • 225
  • 3
  • 13

2 Answers2

1

If you want to run code right after your each you need to do it like so:

We check the length of the array you are looping through, if its the the same as the length of the array, you're at the end!

No need to use count_jersey now either.

var J_lis = $("#Jerseybox ul li img");
J_lis.hide();
J_lis.each(function(g) {
    $(this).delay((g + 1) * 360).fadeIn();
    if (g === J_lis.length - 1 ) {
     // here you have your last image!
    }
});
omarjmh
  • 13,632
  • 6
  • 34
  • 42
  • hmm it didt work , i tryed using console.log to check noting show. – darkness Mar 31 '16 at 15:16
  • can you mark this as the correct answer? the code above is still wrong, might mislead people. Wrong as in, the change you had to make to get it to work is already in my answer – omarjmh Mar 31 '16 at 15:33
  • Your question is still wrong because it doesn't run the `if statement` after the `fadeIn` function is finished running. – Devplex Mar 31 '16 at 15:36
  • I have no idea what that has to do with the question he asked and its wrong anyways, are you telling me code in the if statement wont run after the fadeIn? – omarjmh Mar 31 '16 at 15:40
-1

You could do something like:

var J_lis = $("#Jerseybox ul li img");
var count_jersey = 0
J_lis.hide();
J_lis.each(function(g) {
 //after fadeIn, increase counter
 //and check for g == J_lis.length
 $(this).delay((g + 1) * 360).fadeIn('fast', function(){
  counter_jersey++;
  //if last image is finished
  if(g == J_lis.length){
    //do something
  }
 });
});

I don't know why you need counter_jersey, but I included it anyway.

Devplex
  • 247
  • 2
  • 8
  • i tried , but those image still fadeIn ,maybe is because the delay i add. – darkness Mar 31 '16 at 15:17
  • i mean the count_jersey == J_lis.length is already trigger but the Image still havet finish fadeIn , i think is becasue the delay. i need to make sure the last Image finish fadeIn , then i can trigger to remove something. – darkness Mar 31 '16 at 15:18
  • its not because of the delay, try the code I wrote now, should work. – Devplex Mar 31 '16 at 15:21
  • Thank ! ~ is what i need but i add the J_lis.length -1 in order to make it work. thank you so much appreciate. – darkness Mar 31 '16 at 15:30