我试图让脚本暂停大约 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/