我有一些使用jquery的代码,我想等待300ms,然后更改变量.我尝试了setTimeout但它不起作用,它只是让变量立即改变.
setTimeout(animationWait = 0, 300);
Run Code Online (Sandbox Code Playgroud)
(我在文档中全局定义了animationWait)基本上我要做的就是等待点击结束,然后才能完成另一次点击.所以我想我会设置一个变量,然后等待300ms,
$('#up-arrow').live('click', function(e) {
if(animationWait == 0) {
animationWait = 1;
.... /* Code */
}
}
Run Code Online (Sandbox Code Playgroud)
因此我需要在延迟运行代码后再将animationWait更改回0.我已经尝试了很多东西,但它还没有用,有什么想法吗?
你没有正确使用setTimeout.必须传递函数名或匿名函数.
//anonymous function
setTimeout( function() { animationWait = 0 }, 300);
//or give it a function name
function change() { animationWait = 0; }
setTimeout(change, 300);
Run Code Online (Sandbox Code Playgroud)