Skip to content Skip to sidebar Skip to footer

Best Way To Put Delay After Calling Javascript Functions

I'm using jquery to call some javascript functions with a delay between them. Also I'm using Jquery Wait When I call below function,all functions are called recpectively,there are

Solution 1:

If you want to call a function every 5 seconds use

setTimeout(function(){f1},5000);
setTimeout(function(){f2},10000);
setTimeout(function(){f2},15000);

if you want to call each function 5 seconds after the last one terminated use

setTimeout(function(){f1;setTimeout(function(){f2;setTimeout(function(){f3},5000);},5000);},5000);

Solution 2:

Here is a function that calls in sequence an array of function:

$.fn.callFn = function(fns, delay) {
    var fn, that = this;
    if(fns.length > 0){
        fn = fns.shift()
        fn && fn();
        setTimeout(function(){
            that.callFn(fns, delay);
        }, delay);
    }
    returnthis;
};

And you would call it like that:

$(this).callFn([f1, f2, f3], 2000);

Solution 3:

$('#box').slideUp(300).delay(800).fadeIn(400);

/* .delay = wait time = 800 (this means it will wait 800/1000 of a second/ "1000 = 1 second") */

Post a Comment for "Best Way To Put Delay After Calling Javascript Functions"