JavaScript版本sleep()
为“ await”。等待功能会暂停当前的aync功能。
您可以尝试运行以下代码以在JavaScript中实现睡眠
<!DOCTYPE html> <html> <body> <script> function setSleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function Display() { document.write('Wait for 5 seconds!'); await setSleep(5000); document.write('After 5 seconds!'); } Display(); </script> </body> </html>