在JavaScript中,Java的Thread.sleep()相当于什么? [英] What's the equivalent of Java's Thread.sleep() in JavaScript?

查看:99
本文介绍了在JavaScript中,Java的Thread.sleep()相当于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,Java的 Thread.sleep()相当于什么?

What's the equivalent of Java's Thread.sleep() in JavaScript?

推荐答案

简单的答案是没有这样的功能。

The simple answer is that there is no such function.

你最接近的是:

var millisecondsToWait = 500;
setTimeout(function() {
    // Whatever you want to do after the wait
}, millisecondsToWait);

请注意,尤其不想忙等待(例如在一个旋转循环中),因为你的浏览器几乎肯定会在单线程环境中执行你的JavaScript。

Note that you especially don't want to busy-wait (e.g. in a spin loop), since your browser is almost certainly executing your JavaScript in a single-threaded environment.

这里有一些其他的SO问题来处理线程JavaScript:

Here are a couple of other SO questions that deal with threads in JavaScript:

  • JavaScript and Threads
  • Why doesn't JavaScript support multithreading?

这个问题也许有用:

  • setTimeout - how to avoid using string for callback?

这篇关于在JavaScript中,Java的Thread.sleep()相当于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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