Simplified solution: increase the waiting time each time.
var i, factor, duration = 250, delayTime = 500; for (i = 2, factor = 0; i <= 4; i++, factor++) { galleryAnimation(i, factor); } function galleryAnimation(i, factor) { $("#pic" + i).delay(factor * delayTime).fadeIn(duration); }
This works the same way your approach does, only the delays get longer every time.
General solution 1 - work with setInterval() so that your working function (the one that executes fadeIn ) is called at predefined intervals:
var elements = $("#pic2,#pic3,#pic4").toArray(), // or any other way to select your elements duration = 250, delayTime = 500, intervalId = setInterval(function () { $(elements.shift()).fadeIn(duration); if (elements.length === 0) { clearInterval(intervalId); } }, delayTime);
General solution 2 - work with callbacks that are called upon completion of the previous animation:
var elements = $("#pic2,#pic3,#pic4").toArray(), // or any other way to select your elements duration = 250, delayTime = 500, next = function () { $(elements.shift()).delay(delayTime).fadeIn(duration, next); }; next(); // start the chain
source share