Home javascript How to pause anywhere in js code?

How to pause anywhere in js code? [duplicate]

Author

Date

Category

I am writing a small userscript in js for an online game, a small automation of routine actions (bot).
There was a need to make delays anywhere in the code after performing an action.
For example, something like delay () in delphi, pascal .. Stops the program execution for the number of milliseconds specified in the parameter (1000 milliseconds in 1 second).

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


Answer 1, authority 100%

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

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions