How to create a sleep/delay in nodejs that is Blocking?

后端 未结 12 639
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 07:00

I\'m currently trying to learn nodejs and a small project I\'m working is writing an API to control some networked LED lights.

The microprocessor controlling the LED

12条回答
  •  独厮守ぢ
    2020-12-02 07:30

    Just use child_process.execSync and call the system's sleep function.

    //import child_process module
    const child_process = require("child_process");
    // Sleep for 5 seconds
    child_process.execSync("sleep 5");
    
    // Sleep for 250 microseconds
    child_process.execSync("usleep 250");
    
    // Sleep for a variable number of microseconds
    var numMicroSeconds = 250;
    child_process.execFileSync("usleep", [numMicroSeconds]);
    

    I use this in a loop at the top of my main application script to make Node wait until network drives are attached before running the rest of the application.

提交回复
热议问题