Execute script after specific delay using JavaScript

前端 未结 15 1760
陌清茗
陌清茗 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 12:41

    I really liked Maurius' explanation (highest upvoted response) with the three different methods for calling setTimeout.

    In my code I want to automatically auto-navigate to the previous page upon completion of an AJAX save event. The completion of the save event has a slight animation in the CSS indicating the save was successful.

    In my code I found a difference between the first two examples:

    setTimeout(window.history.back(), 3000);
    

    This one does not wait for the timeout--the back() is called almost immediately no matter what number I put in for the delay.

    However, changing this to:

    setTimeout(function() {window.history.back()}, 3000);
    

    This does exactly what I was hoping.

    This is not specific to the back() operation, the same happens with alert(). Basically with the alert() used in the first case, the delay time is ignored. When I dismiss the popup the animation for the CSS continues.

    Thus, I would recommend the second or third method he describes even if you are using built in functions and not using arguments.

    0 讨论(0)
  • 2020-11-22 12:42

    There is the following:

    setTimeout(function, milliseconds);
    

    function which can be passed the time after which the function will be executed.

    See: Window setTimeout() Method.

    0 讨论(0)
  • 2020-11-22 12:42

    To add on the earlier comments, I would like to say the following :

    The setTimeout() function in JavaScript does not pause execution of the script per se, but merely tells the compiler to execute the code sometime in the future.

    There isn't a function that can actually pause execution built into JavaScript. However, you can write your own function that does something like an unconditional loop till the time is reached by using the Date() function and adding the time interval you need.

    0 讨论(0)
  • 2020-11-22 12:44

    Just to expand a little... You can execute code directly in the setTimeout call, but as @patrick says, you normally assign a callback function, like this. The time is milliseconds

    setTimeout(func, 4000);
    function func() {
        alert('Do stuff here');
    }
    
    0 讨论(0)
  • 2020-11-22 12:45

    delay function:

    /**
     * delay or pause for some time
     * @param {number} t - time (ms)
     * @return {Promise<*>}
     */
    const delay = async t => new Promise(resolve => setTimeout(resolve, t));
    

    usage inside async function:

    await delay(1000);
    
    0 讨论(0)
  • 2020-11-22 12:46

    As other said, setTimeout is your safest bet
    But sometimes you cannot separate the logic to a new function then you can use Date.now() to get milliseconds and do the delay yourself....

    function delay(milisecondDelay) {
       milisecondDelay += Date.now();
       while(Date.now() < milisecondDelay){}
    }
    
    alert('Ill be back in 5 sec after you click OK....');
    delay(5000);
    alert('# Im back # date:' +new Date());

    0 讨论(0)
提交回复
热议问题