如何停止/重写一个jQuery超时功能? [英] How to stop/override a Jquery TimeOut function?

查看:153
本文介绍了如何停止/重写一个jQuery超时功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的jquery片段,在屏幕的顶部响应于网页上的用户操作显示通知消息。通知里面坐了动态内容的Ajax动作后,往往显示出来。

I have a small jquery snippet that displays notification message at the top of the screen in response to user actions on a page. The notification is often displayed after Ajax actions with dynamic content inside it.

例如:

$("#mini-txt").html("Thank you!");
$("#mini").fadeIn("fast");
setTimeout(function() {$("#mini").animate({height: "hide", opacity: "hide"}, "medium");}, 3000);

的通知效果很好,当用户确实在快速连续两个或两个以上的行为,在这种情况下,超时功能会混淆自己和出现的第二个消息进来了previous 3000毫秒的除外。

The notification works well, except when a user does two or more actions in rapid succession, in which case the TimeOut function will confuse itself and the second message appears to come inside the previous 3000 milliseconds.

有没有一种方法,如果执行新的行动,以杀了previous通知。我有与动作/选择器,只是超时功能没有问题....要么停止,或以某种方式覆盖它。或者,也许有一个为获取消息萦绕在屏幕几秒钟就消失之前?

Is there a way to "kill" the previous notification if a new action is performed. I've got no problem with the actions/selectors, just the TimeOut function.... either stopping it or overriding it somehow. Or perhaps there's a better alternative for getting the message to linger on the screen for a few seconds before disappearing?

感谢你。

推荐答案

首先,您存储为的setTimeout 函数的返回值:

First, you store the return value for the setTimeout function:

// Set the timeout
var timeout = setTimeout(function()
    {
        // Your function here
    }, 2000);

然后,当您准备好要杀死超时......你刚才叫 clearTimeout 从previous调用存储的值的setTimeout

Then, when you're ready to kill the timeout...you just call clearTimeout with the stored value from the previous call to setTimeout.

// Then clearn the timeout
clearTimeout(timeout);

这篇关于如何停止/重写一个jQuery超时功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆