简体   繁体   English

如何等待一段时间然后更改变量?

[英]How can I wait a while and then change variable?

I have a little code using jquery and I want to wait like, 300ms and then change a variable. 我有一些使用jquery的代码,我想等待300ms然后更改一个变量。 I tried setTimeout but it wouldn't work, it just made the variable change instantly. 我尝试了setTimeout,但是它不起作用,它只是使变量立即更改。

setTimeout(animationWait = 0, 300);

(I defined animationWait earlier globally in the document) Basically what I'm trying to do is wait for a click to end before another click can be done. (我在文档的前面部分中全局定义了animationWait)基本上,我想做的是等待单击结束,然后再单击一次。 So i thought I'd set a variable and then wait 300ms, 所以我想我先设置一个变量,然后等待300ms,

$('#up-arrow').live('click', function(e) {  

    if(animationWait == 0) {
        animationWait = 1;
        .... /* Code */
    }
}

So I need to change the animationWait back to 0 again after a delay to run the code. 所以我需要在延迟运行代码后再次将animationWait更改回0。 I've tried a bunch of stuff but it's still not working, any ideas? 我尝试了很多东西,但仍然无法正常工作,有什么想法吗?

You are not using setTimeout quite right. 您没有正确使用setTimeout。 It must be passed either a function name or an anonymous function. 必须将其传递给函数名称或匿名函数。

//anonymous function
setTimeout( function() { animationWait = 0 }, 300);

//or give it a function name
function change() { animationWait = 0; }
setTimeout(change, 300);

The parameter what you pass should be an evaluation statement. 您传递的参数应该是评估语句。 For me this works fine: 对我来说,这很好:

var animationWait = -1;    //Initially set the value to -1
alert(animationWait);      //alert to see the value
setTimeout("animationWait = 0", 300);    //Setting the value to 0
setTimeout("alert(animationWait)", 400);  //Alert after 400 ms

Have you tried using jQuery function delay() ? 您是否尝试过使用jQuery函数delay()

A standard use case would look something like this: 一个标准的用例看起来像这样:

$('item').delay(300).delayedEffectFunction();

Where delayedEffectFunction() is a made-up function you want to be delayed. 其中delayedEffectFunction()是要延迟的delayedEffectFunction()函数。

The answer to your question as asked is that setTimeout() expects the first parameter to be a function reference - either the name of a function declared elsewhere or an anonymous function. 您所提出的问题的答案是setTimeout()希望第一个参数是函数引用-在其他地方声明的函数名称或匿名函数。 (Or a string, but for several reasons using a string is almost always the wrong solution.) Your code as is simply executes the expression animationWait = 0 and passes the result of that expression (0) to setTimeout() . (或者是一个字符串,但是出于多种原因,使用字符串几乎总是错误的解决方案。)您的代码照原样执行表达式animationWait = 0并将该表达式(0)的结果传递给setTimeout()

So to execute your single line of code via setTimeout() I'd recommend wrapping it in an anonymous function and passing that function to setTimeout() : 因此,要通过setTimeout()执行单行代码,我建议将其包装在一个匿名函数中,并将该函数传递给setTimeout()

setTimeout(function(){ animationWait = 0; }, 300);

However, if you are doing animation with jQuery its animation methods allow you to pass a callback function to be executed when the animation ends, so you can do this type of processing there rather than independently setting your own timeout. 但是,如果您正在使用jQuery进行动画处理,则其动画方法允许您传递在动画结束时执行的回调函数,因此您可以在此处进行这种类型的处理,而不必独立设置自己的超时。 For example, if your animation was like this: 例如,如果您的动画是这样的:

$("#someelement").animate({someproperty:"somevalue"},
                          300);

Then you could add a callback function that will be called after this animation completes: 然后,您可以添加一个回调函数,该动画完成后将被调用:

$("#someelement").animate(({someproperty:"somevalue"},
                          300,
                          function(){ animationWait = 0; });

To my eye this is neater than a separate setTimeout() call, but also it is easier to maintain because the duration only has to be specified in one place rather than you having to sync your own timeout to the animation. 在我看来,这比单独的setTimeout()调用更整洁,但也更易于维护,因为持续时间只需要在一个地方指定,而不必将自己的超时同步到动画。

A similar syntax applies to other jQuery methods like .fadeIn() , .hide() , etc. See the .animate() doco for more information and examples. 类似的语法适用于其他jQuery方法等.fadeIn() .hide()等等。参见.animate() DOCO以获取更多信息和例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM