Execute script after specific delay using JavaScript

前端 未结 15 1761
陌清茗
陌清茗 2020-11-22 12:29

Is there any JavaScript method similar to the jQuery delay() or wait() (to delay the execution of a script for a specific amount of time)?

相关标签:
15条回答
  • 2020-11-22 13:03

    You need to use setTimeout and pass it a callback function. The reason you can't use sleep in javascript is because you'd block the entire page from doing anything in the meantime. Not a good plan. Use Javascript's event model and stay happy. Don't fight it!

    0 讨论(0)
  • 2020-11-22 13:07

    Just to add to what everyone else have said about setTimeout: If you want to call a function with a parameter in the future, you need to set up some anonymous function calls.

    You need to pass the function as an argument for it to be called later. In effect this means without brackets behind the name. The following will call the alert at once, and it will display 'Hello world':

    var a = "world";
    setTimeout(alert("Hello " + a), 2000);
    

    To fix this you can either put the name of a function (as Flubba has done) or you can use an anonymous function. If you need to pass a parameter, then you have to use an anonymous function.

    var a = "world";
    setTimeout(function(){alert("Hello " + a)}, 2000);
    a = "Stack Overflow";
    

    But if you run that code you will notice that after 2 seconds the popup will say 'Hello Stack Overflow'. This is because the value of the variable a has changed in those two seconds. To get it to say 'Hello world' after two seconds, you need to use the following code snippet:

    function callback(a){
        return function(){
            alert("Hello " + a);
        }
    }
    var a = "world";
    setTimeout(callback(a), 2000);
    a = "Stack Overflow";
    

    It will wait 2 seconds and then popup 'Hello world'.

    0 讨论(0)
  • 2020-11-22 13:07

    I had some ajax commands I wanted to run with a delay in between. Here is a simple example of one way to do that. I am prepared to be ripped to shreds though for my unconventional approach. :)

    //  Show current seconds and milliseconds
    //  (I know there are other ways, I was aiming for minimal code
    //  and fixed width.)
    function secs()
    {
        var s = Date.now() + ""; s = s.substr(s.length - 5);
      return s.substr(0, 2) + "." + s.substr(2);
    }
    
    //  Log we're loading
    console.log("Loading: " + secs());
    
    //  Create a list of commands to execute
    var cmds = 
    [
        function() { console.log("A: " + secs()); },
        function() { console.log("B: " + secs()); },
        function() { console.log("C: " + secs()); },
        function() { console.log("D: " + secs()); },
        function() { console.log("E: " + secs()); },
      function() { console.log("done: " + secs()); }
    ];
    
    //  Run each command with a second delay in between
    var ms = 1000;
    cmds.forEach(function(cmd, i)
    {
        setTimeout(cmd, ms * i);
    });
    
    // Log we've loaded (probably logged before first command)
    console.log("Loaded: " + secs());
    

    You can copy the code block and paste it into a console window and see something like:

    Loading: 03.077
    Loaded: 03.078
    A: 03.079
    B: 04.075
    C: 05.075
    D: 06.075
    E: 07.076
    done: 08.076
    
    0 讨论(0)
提交回复
热议问题