How to add delay before calling next call back function?

笑着哭i 提交于 2019-12-05 14:37:10

Okay, try this. After your animations:

$("#img1").fadeOut(AnimTime);
$("#img2").fadeIn(AnimTime);
setTimeout(Show3, 30000); //delays next call for 30 seconds

What I do for this is here in this gist: http://gist.github.com/467030

Essentially it lest you create a completely unrelated array of functions, animations or not... and then execute them one by one at the given interval.

// create an array of functions to be executed
// everything in each step would be executed simultaneously
var myFuncs = [
    // Step #1
    function () {
        $("#img1").fadeOut(200);
        doSomething();
        doSomethingElseAtTheSameTime();
    },
    // Step #2
    function () {
        doOtherStuff();
    },
    // Step #3
    function () {
        woohoo();
    }
];

// then, the function in that gist: 
// Simple function queue runner. Just pass me an array of functions and I'll
// execute them one by one at the given interval.
var run_queue = function (funcs, step, speed) {
step = step || 0;
speed = speed || 500;
funcs = funcs || [];

    if (step < funcs.length) {
        // execute function
        funcs[step]();

        // loop it
        setTimeout(function () {
            run_queue(funcs, step + 1);
        }, speed);
    }

    return;
};

// run them.
run_queue(myFuncs, 0, 1000);

Obviously, this is simpler than you'd want. But the basic idea works really well. Even using jQuery queue() only works for performing subsequent animations on the same items. These can be completely unrelated function executions.

try this

$("#img3").delay('1000').fadeOut(AnimTime);

You have to do a sleep function take a look here it's a jQuery plygin

usage:

$.sleep(3, function(){alert("I slept for 3 seconds!");});

use $("#img3").fadeIn(AnimTime).delay('1000')

1000 is in milliseconds.

setTimeout(MyFunction(), 3000);

I would do this to simply pause 3 seconds before executing the MyFunction. Hope this helps...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!