等待

标签 javascript html wait

我试图让脚本暂停大约 1 秒,然后继续执行脚本,但我似乎无法弄清楚如何做。这是我的代码:

function hello() {
  alert("Hi!")
  //I need about a 1 seconed pause here;
  alert("Hi again!");
}
<button onclick="hello()">Say Hi!</button>

我已经尝试过以下方法:

function hello() {
  alert("Hi!")
  setTimeout(myFunction, 3000)//I thought this would wait, run the function, then return to the function hello()...
  alert("Hi again!")
}
function myFunction {
  a = document.getElementById("blank")
  a.innerHTML = "wait complete"
}
<button onclick="hello()">Say Hi!</button>
<div id="blank">
...
</div>

最佳答案

您使用 setTimeout 函数走在正确的轨道上:

function hello() {
  alert("Hi!")
  setTimeout(function() {
    alert("Hi again!");
  }, 1000)

}
<button onclick="hello()">Say Hi!</button>

关于等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53403262/

相关文章:

javascript - ES6 中允许类生成函数吗?

javascript - Object.assign 是如何工作的?

javascript - 如何在单击菜单项时关闭 Twitter Bootstrap 3 折叠导航栏

javascript - 使用javascript,如何在单击时获取表格单元格的背景颜色?

c - wait() 不会等待每个 child

javascript - VM299 :1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON. 解析(<匿名>)

Javascript - for循环仅打印最后一次迭代

html - Bootstrap 水平导航栏不工作。显示为垂直

Android - 在继续之前等待动画完成?

linux - 我可以在 bash 中为多个并行后台进程使用相同的变量名吗?