延迟/睡眠在JavaScript? [英] delay/sleep in javascript?

查看:106
本文介绍了延迟/睡眠在JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法可以在Javascript中暂停脚本?我在Python中寻找等价于 time.sleep(1)的javascript。我不希望 setTimeout(continueExecution,10),或任何 getTime 。可以这样做吗?

Is there a simple way to pause the script in Javascript? I'm looking for the javascript equivalent of time.sleep(1) in Python. I do NOT want setTimeout(continueExecution, 10), or anything with getTime. Can this be done?

编辑:这不是那么容易,但我想我可以用 setTimeout

It wasn't quite as easy, but I think I can get it done with setTimeout

推荐答案

JavaScript通常使用事件循环在单个线程中运行。这就是为什么你不能做任何Thread.sleep。如果可能,它会冻结其他一切,并相信我你不想那样做。这就是为什么JavaScript中的几乎所有内容都使用非阻塞IO运行的原因,这就是为什么只能通过使用setTimeout或setInterval来执行延迟执行的原因。

JavaScript is usually ran in a single thread using an event-loop. This is why you can't do any "Thread.sleep". If you could it would freeze everything else for that duration and believe me you don't want to do that. This is why almost everything in JavaScript runs using non-blocking IO and that's why the only way you can do delay execution is by using setTimeout or setInterval.

这篇关于延迟/睡眠在JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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