What is the JavaScript equivalent of the PHP sleep() function?

usesetTimeout()

This setTimeout()function is a built-in JavaScript function that allows you to run a piece of code after a specified amount of time. You can use this to create delays in your scripts. Here is an example:

console.log('Before Sleep');
setTimeout(() => {
  console.log('After Sleep');
}, 3000);

In the above example, console.log('Before Sleep')the statement will be executed immediately, and then there will be a delay of 3 seconds, after which the statement console.log('After Sleep')will be executed.

useasync/await

Another way to create delays in JavaScript is to useasync/await

おすすめ

転載: blog.csdn.net/weixin_38612163/article/details/129464870