using setTimeout synchronously in JavaScript

后端 未结 9 1705
无人及你
无人及你 2020-12-02 12:40

I have the following scenario:

setTimeout(\"alert(\'this alert is timedout and should be the first\');\", 5000);
alert(\"this should be the second one\");


        
相关标签:
9条回答
  • 2020-12-02 13:18

    I came in a situation where I needed a similar functionality last week and it made me think of this post. Basically I think the "Busy Waiting" to which @AndreKR refers, would be a suitable solution in a lot of situations. Below is the code I used to hog up the browser and force a wait condition.

    function pause(milliseconds) {
    	var dt = new Date();
    	while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
    }
    
    document.write("first statement");
    alert("first statement");
    
    pause(3000);
    
    document.write("<br />3 seconds");
    alert("paused for 3 seconds");

    Keep in mind that this code acutally holds up your browser. Hope it helps anyone.

    0 讨论(0)
  • 2020-12-02 13:24

    I think you have to make a promise and then use a .then() so that you can chain your code together. you should look at this article https://developers.google.com/web/fundamentals/primers/promises

    0 讨论(0)
  • 2020-12-02 13:25

    No, as there is no delay function in Javascript, there is no way to do this other than busy waiting (which would lock up the browser).

    0 讨论(0)
  • 2020-12-02 13:31

    Is the code contained in a function?

    function test() {
        setTimeout(...);     
    
        // code that you cannot modify?
    }
    

    In that case, you could prevent the function from further execution, and then run it again:

    function test(flag) {
    
        if(!flag) {
    
            setTimeout(function() {
    
               alert();
               test(true);
    
            }, 5000);
    
            return;
    
        }
    
        // code that you cannot modify
    
    }
    
    0 讨论(0)
  • 2020-12-02 13:34

    Using ES6 & promises & async you can achieve running things synchronously.

    So what is the code doing?

     1. Calls setTimeOut 1st inside of demo then put it into the webApi Stack
     2. Creates a promise from the sleep function using the setTimeout, then resolves after the timeout has been completed;
     3. By then, the first setTimeout will reach its timer and execute from webApi stack. 
     4. Then following, the remaining alert will show up.
    
    
    function sleep(ms) {
      return new Promise(resolve => setTimeout(resolve, ms));
    }
    
    async function demo() {
      setTimeout("alert('this alert is timedout and should be the first');", 5000);
      await sleep(5000);
      alert('this should be the second one');
    }
    demo();
    
    0 讨论(0)
  • 2020-12-02 13:34

    ES6 (busy waiting)

    const delay = (ms) => {
      const startPoint = new Date().getTime()
      while (new Date().getTime() - startPoint <= ms) {/* wait */}
    }
    

    usage:

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