如何停止/覆盖 Jquery TimeOut 函数?
我有一个小的 jquery 片段,它在屏幕顶部显示通知消息,以响应页面上的用户操作。通知通常在 Ajax 操作之后显示,其中包含动态内容。
例如:
$("#mini-txt").html("Thank you!");
$("#mini").fadeIn("fast");
setTimeout(function() {$("#mini").animate({height: "hide", opacity: "hide"}, "medium");}, 3000);
通知效果很好,除非用户快速连续执行两个或多个操作,在这种情况下,TimeOut 函数会混淆自身,并且第二条消息似乎是在前 3000 毫秒内出现的。
如果执行新操作,是否有办法“杀死”先前的通知。我对操作/选择器没有任何问题,只是超时功能......要么停止它,要么以某种方式覆盖它。或者也许有更好的选择让消息在屏幕上停留几秒钟然后消失?
谢谢。
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.
For example:
$("#mini-txt").html("Thank you!");
$("#mini").fadeIn("fast");
setTimeout(function() {$("#mini").animate({height: "hide", opacity: "hide"}, "medium");}, 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.
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?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,存储
setTimeout
函数的返回值:然后,当您准备好终止超时时...您只需使用存储的值调用
clearTimeout
之前对setTimeout
的调用。First, you store the return value for the
setTimeout
function:Then, when you're ready to kill the timeout...you just call
clearTimeout
with the stored value from the previous call tosetTimeout
.您可以使用 .stop()
You can use .stop()
jQuery 1.4 有一个内置方法来处理动画延迟,您可以执行以下操作:
然后当您想要清理动画队列时,您可以执行以下操作:
jQuery 1.4 has a built in method to handle delays for animations you can do something like this:
And then later when you want to clean the animation queue you can do:
这将在函数运行延迟 500ms 后清除超时
This will clear timeout after function run with delay 500ms