我在使用 setTimeout()
函数时遇到问题。到目前为止,这是我的代码:
$('#start').click(function(event) {
var result = Math.floor(Math.random() * (10000 - 4000 + 1)) + 4000; // creates a random value between 4000 and 10000 and store it in a variable
setTimeout("$('.reflex').css('background','red')",result); // this works great
setTimeout(startTimer(),result); //this code triggers without delay
});
明白了吗?在 setTimeout 中使用函数时,延迟不适用,函数会立即执行。
我能做什么?
最佳答案
setTimeout
接受一个函数,而不是函数的结果作为它的第一个参数。
setTimeout(startTimer,result);
注意,startTimer
后没有()
。
关于jquery - setTimeout 无法使用其中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28305440/