//--Need a way to set a delay here--\\\
var result = ['Rock', 'Paper', 'Scissors',][Math.floor(Math.random() * 3)]
cpuReply.value = result
//--end of script--\\\
好吧,我正在尝试找到一种方法在这两行代码之前添加延迟。我听过很多关于使用 .delay 的建议,但我不知道如何定义它,如果我这样做,它经常会说意外的数字
delay(5000)
var result = ['Rock', 'Paper', 'Scissors',][Math.floor(Math.random() * 3)]
cpuReply.value = result
我不确定我输入的延迟是否正确(如果错误请告诉我),请帮我解决这个问题。
最佳答案
window.setTimeout 就是您正在寻找的。
请记住它是异步的,因此其他事情可以(并且将会)在超时函数之外执行。 setTimeout
优于其他选项,因为它是“非阻塞”的。 IE:WAITING时不会导致网页卡住。
记住:setTimeout 不应该用来测量时间。这是因为当超时完成时,函数将被添加到调用堆栈中,因此您不能保证它会立即执行。
window.setTimeout(function(){
var result = ['Rock', 'Paper', 'Scissors',][Math.floor(Math.random() * 3)]
cpuReply.value = result
}, 5000);// time is in 1000ths of a second
关于javascript - 如何在 JavaScript 中设置延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33070150/