How does node sleep for a few seconds

  node.js, question

No sleep

Kid, let me save you, haha, are you sleeping or are you synchronized? ? The following code will not be executed, and several methods are provided for you to choose from.

  • SetTimeout, put all your programs to be executed in the next few seconds into setTimeout

setTimeout(fun, 3000);
  • There are npm packages available.sleep

var sleep = require('sleep');
 sleep.sleep(n): sleep for n seconds

Similar libraries includewaitfor,suspendAndgen-runWait, choose casually, of course sleep will be relatively simple.

  • I wrote it myself, that is, using a dead cycle to control Central Processor and prevent it from being idle will also meet your needs. However, Central Processor will be under great pressure. It is not recommended, but you can understand it.

//sleep for 3s
 Varwaituntil = newdate (newdate (). gettime ()+3 * 1000);
 while(waitUntil > new Date()){}
  • Last but not the lead: process. next tick (), but may not be as precise as the number of seconds required to meet your requirements. if the time needs to be accurately controlled, then ignore this, just let you know that there is such a method.

Finally, please L’amour en douce! ! ! Ha-ha