This question has already been answered:

I write small userscript on js for online games, small automation of routine actions (bot). There was a need to make delays anywhere in the code after performing any action. For example, something like delay () in Delphi, Pascal .. Stops program execution for the number of milliseconds specified in the parameter (1000 milliseconds in 1 second).

MB someone faced with a similar task mb wrote a similar delay function. setTimeout and setInterval are not suitable as they are heavily cluttering up the code if you constantly insert them and start to get confused with time, but the delay () function anywhere in the code would be perfect. Are there any third-party libraries for solving such problems?

Reported as a duplicate at Grundy. javascript Mar 17 '18 at 7:45 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    Solution on enSO: stackoverflow.com/questions/951021/… - Kromster
  • one
    setTimeout is the only option. If there are any libraries, then these are just wrappers over setTimeout - Darth
  • @Darth Thank you, that's right! - Mikhail Petrov

1 answer 1

function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function demo() { console.log('Taking a break...'); await sleep(2000); console.log('Two second later'); } demo();