如何停止/覆盖 Jquery TimeOut 函数? [英] How to stop/override a Jquery TimeOut function?

查看:30
本文介绍了如何停止/覆盖 Jquery TimeOut 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的 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);

通知效果很好,除非用户快速连续执行两个或多个操作,在这种情况下 TimeOut 函数会混淆自身,并且第二条消息似乎在前 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.

如果执行新操作,有没有办法杀死"之前的通知.我对动作/选择器没有任何问题,只是 TimeOut 功能......要么停止它,要么以某种方式覆盖它.或者也许有更好的选择让消息在消失之前在屏幕上停留几秒钟?

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);

然后,当您准备好终止超时时……您只需使用上次调用 setTimeout 的存储值调用 clearTimeout.

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 TimeOut 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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