setTimeout无法使用其中的函数

时间:2015-02-03 17:48:36

标签: jquery html css

我遇到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中使用函数时,延迟不适用,函数立即执行。

我该怎么办?

1 个答案:

答案 0 :(得分:3)

setTimeout接受一个函数,而不是函数的结果作为其第一个参数。

setTimeout(startTimer,result);

注意,()之后没有startTimer

相关问题