jquery - setTimeout 无法使用其中的函数

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

我能做什么?

最佳答案

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

setTimeout(startTimer,result);

注意,startTimer 后没有()

关于jquery - setTimeout 无法使用其中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28305440/

相关文章:

javascript - 提交内容后删除文本字段

html - 格式化我的网站

javascript - fadeOut+delay 不能正常工作

jquery - Reload $(document).ready(function() ajax页面重新加载后

javascript - 如何从两个数组动态构建 ul li - Jquery

javascript - 谷歌地图方向服务零结果

HTML5 视频标签控件 ="false"- 控件在暂停时显示

javascript - JS 正则表达式从文本区域中删除 "too much"

php - 使用 PHP 在屏幕上创建点

javascript - 如何处理 catch 函数?