如何让我的 JavaScript 在运行下一条语句之前等待 0.5 秒?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25221243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:11:24  来源:igfitidea点击:

How can I make my JavaScript wait 0.5 seconds before running the next statement?

javascript

提问by Heavy Rain

I want to do this

我想做这个

statement1;
// rest for 0.5 seconds
statement2;

How can I make my JavaScript wait 0.5 seconds?

如何让我的 JavaScript 等待 0.5 秒?

I tried

我试过

statement1;
window.setTimer(500);
statement2;

But that was not it. Thanks

但事实并非如此。谢谢

回答by John Sterling

statement1();
setTimeout(function() {
    statement2();
}, 500);

回答by EdwardB

You can put a timer like this statement1();

你可以放一个像这样的计时器 statement1();

setTimeout(
  function() 
  {
        statement2();
  }, 500);

How to wait 5 seconds with jQuery?

如何使用 jQuery 等待 5 秒?