Synchronous delay in code execution

前端 未结 10 1177
长情又很酷
长情又很酷 2020-12-03 02:29

I have a code which needs to be executed after some delay say 5000 ms.Currently I am using setTimeout but it is asynchronous and i want the execution to wait for its return.

相关标签:
10条回答
  • 2020-12-03 02:59

    JavaScript is a single-threaded language. You cannot combine setTimeout and synchronous processing. What will happen is, the timer will lapse, but then the JS engine will wait to process the results until the current script completes.

    If you want synchronous methods, just call the method directly!

    If you want to process something after the setTimeout, include it or call it from the timeout function.

    0 讨论(0)
  • Here's how you can use the JQuery doTimeout plugin

    jQuery('selector').doTimeout( [ id, ] delay, callback [, arg ... ] );
    

    From the docs: "If the callback returns true, the doTimeout loop will execute again, after the delay, creating a polling loop until the callback returns a non-true value."

    var start = Date.now();
    console.log("start: ", Date.now() - start);
    var i = 0;
    $.doTimeout('myLoop', 5000, function() {
      console.log(i+1, Date.now() - start);
      ++i;
      return i == 5 ? false : true;
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-dotimeout/1.0/jquery.ba-dotimeout.min.js"></script>

    0 讨论(0)
  • 2020-12-03 03:06

    Node solution

    Use fs.existsSync() to delay

    const fs = require('fs');
    const uuidv4 = require('uuid/v4');
    
    /**
     * Tie up execution for at-least the given number of millis.  This is not efficient.
     * @param millis Min number of millis to wait
     */
    function sleepSync(millis) {
        if (millis <= 0) return;
        const proceedAt = Date.now() + millis;
        while (Date.now() < proceedAt) fs.existsSync(uuidv4());
    }
    

    fs.existsSync(uuidv4()) is intended to do a few things:

    1. Occupy the thread by generating a uuid and looking for a non-existent file
    2. New uuid each time defeats the file system cache
    3. Looking for a file is likely an optimised operation that should allow other activity to continue (i.e. not pin the CPU)
    0 讨论(0)
  • 2020-12-03 03:11

    If you'd like to take advantage of the new async/await syntax, You can convert set timeout to a promise and then await it.

    function wait(ms) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          console.log("Done waiting");
          resolve(ms)
        }, ms )
      })
    }  
    
    (async function Main() {
      console.log("Starting...")
      await wait(5000);
      console.log("Ended!")
    })();
    
    0 讨论(0)
  • 2020-12-03 03:11

    Using the new Atomics API, you can start synchronous delays without performance spikes:

    const sleep = milliseconds => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, milliseconds)
    
    sleep(5000) // Sleep for 5 seconds
    
    console.log("Executed after 5 seconds!")
    
    0 讨论(0)
  • 2020-12-03 03:12

    I have made a simple synchronous timeout function. It works in two different ways, callback and non-callback.

    function:

    function wait(ms, cb) {
      var waitDateOne = new Date();
      while ((new Date()) - waitDateOne <= ms) {
        //Nothing
      }
      if (cb) {
        eval(cb);
      }
    }
    

    callback example:

    wait(5000,"doSomething();");
    

    non-callback example:

    console.log("Instant!");
    wait(5000);
    console.log("5 second delay");
    
    0 讨论(0)
提交回复
热议问题