JavaScript 计时问题
我不会在一定时间内运行一段代码,然后在完成后继续运行另一段代码。
I wont to run a block of code in a certain amount of time and then when done, carry on with another block of code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 setTimeout() 可能就是您想要的。 例如...
希望有帮助
Using the setTimeout() is probably what you want. For example...
Hope it helps
这就是您的做法,使用
setTimeout
函数,它将要调用的代码作为第一个参数,以及调用它之前应等待的时间(以毫秒为单位)作为第二个参数:由于 Javascript 的性质,您必须封装要运行的代码在其自己的函数中超时完成后,否则将在超时完成之前运行。 从本质上讲,这是一个回调,它是Javascript 基于事件的性质。
This is how you would do it, using the
setTimeout
function, which takes code to call as the first argument and how much time it should wait before calling it (in milliseconds) as the second argument:Because of the nature of Javascript you have to encapsulate the code you want to run after the timeout is finished in its own function, otherwise it would be run before the timeout is finished. This is, in essense, a callback, and it is a big part of the event-based nature of Javascript.
您需要使用 setTimeout() 函数。
You'll want to use the setTimeout() function.
setTimeout - 在一段时间间隔后执行代码
clearTimeout - 取消 setTimeout()
更多详细信息 此处。
setTimeout - executes code after a time interval
clearTimeout - cancels the setTimeout()
More details here.
使用setTimeout。
setInterval
与setTimeout
类似,只不过它重复执行代码。Use
setTimeout
.and
setInterval
is likesetTimeout
, except it repeats a code repeatedly.