JavaScript中是否有睡眠/暂停/等待功能? [英] Is there a Sleep/Pause/Wait function in JavaScript?

查看:136
本文介绍了JavaScript中是否有睡眠/暂停/等待功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

什么是sleep版本的JavaScript()?

是否有一个JavaScript函数模拟PHP中 sleep 函数的操作 - 这个函数暂停代码执行x毫秒,然后从中断处继续?

Is there a JavaScript function that simulates the operation of the sleep function in PHP — a function that pauses code execution for x milliseconds, and then resumes where it left off?

我在Stack Overflow上找到了一些东西,但没什么用处。

I found some things here on Stack Overflow, but nothing useful.

推荐答案

您需要将代码重新分解为多个部分。这不会停止执行,它只会在各部分之间造成延迟。

You need to re-factor the code into pieces. This doesn't stop execution, it just puts a delay in between the parts.

function partA() {
  ...
  window.setTimeout(partB,1000);
}

function partB() {
   ...
}

这篇关于JavaScript中是否有睡眠/暂停/等待功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆