Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are all the iterations run at the same time?

Here's the jsfiddle simulating my problem relating to this code:

$('#button').click(function(){
   var i;
   for (i = 1; i < 4; ++i) {
      $('#img' + i).fadeIn("slow").delay(1000);
      $('#img' + i).fadeOut("slow");
   }
});

I was expecting the #img1 element to fade in and then for the execution to stop for 1 second and then fade out, then start over for the #img2 element etc.

like image 379
bqui56 Avatar asked Nov 21 '12 04:11

bqui56


People also ask

Are iterations and loops the same?

A loop is defined as a segment of code that executes multiple times. Iteration refers to the process in which the code segment is executed once. One iteration refers to 1-time execution of a loop. A loop can undergo many iterations.

How many times can a while loop run?

There is no fixed answer to how many times the while loop is executed. The while loop is always executed when there is a carry bit from one position to another.

What is the necessity for iterations when solving a problem?

Why is iteration important? Iteration allows us to simplify our algorithm by stating that we will repeat certain steps until told otherwise. This makes designing algorithms quicker and simpler because they don't have to include lots of unnecessary steps.


2 Answers

The reason the animations appear to run at the same time is that jQuery's animations are all performed asynchronously. So what your code is doing is basically starting all of the animations, and then your browser handles the actual animations nearly simultaneously.

jQuery's animation functions do support the use of callbacks which are called after the animation is finished, however. By making sure that the later animations happen in this callback, we can force the animations to execute in sequence.

Here's one way you could implement your requirements (jsfiddle here):

$('#button').click(function(){
    var i;
    var $elems = [];
    for (i = 1; i < 4; ++i) {
        $elems.push($('#img' + i));
    }

    var animate = function () {
        var $elem;
        if ($elems.length) {
            $elem = $elems.shift();
            $elem.fadeIn("slow").delay(1000).fadeOut("slow", animate);
        }
        // if the $elems has been cleared out, we're done and this function won't be requeued
    };

    animate();
});

Note that I'm queuing up another call to animate() as the callback for .fadeOut. The function will then re-execute, but the state of the $elems array will be mutated across each function call so that each element has its animations executed once, in order. When the array is cleared out, the function will run only once more to verify that the elements have all been animated, and in that case it will return without re-queuing itself.

like image 86
Platinum Azure Avatar answered Oct 06 '22 00:10

Platinum Azure


You would want to utilize the callbacks in fadeIn and fadeOut function. Here is an example http://jsfiddle.net/JaQmd/5/ .

The heart of the code is basically calling the callback with the new image number like so

var imageNumber = 0;

$('#button').click(function doAnimation() {
++imageNumber;
if(imageNumber <= 3){
   $("#img" + imageNumber).fadeIn('slow').delay(1000);
   $("#img" + imageNumber).fadeOut('slow', doAnimation);
  }
});​
like image 34
dchhetri Avatar answered Oct 06 '22 00:10

dchhetri