如何停止/覆盖 Jquery TimeOut 函数?

发布于 2024-08-27 12:35:47 字数 470 浏览 4 评论 0原文

我有一个小的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花之痕靓丽 2024-09-03 12:35:48

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

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

然后,当您准备好终止超时时...您只需使用存储的值调用 clearTimeout之前对 setTimeout 的调用。

// Then clearn the timeout
clearTimeout(timeout);

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

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

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);
海拔太高太耀眼 2024-09-03 12:35:48

您可以使用 .stop()

停止当前正在运行的动画
在匹配的元素上。

You can use .stop()

Stop the currently-running animation
on the matched elements.

[旋木] 2024-09-03 12:35:48

jQuery 1.4 有一个内置方法来处理动画延迟,您可以执行以下操作:

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

然后当您想要清理动画队列时,您可以执行以下操作:

$("#mini").stop(true);

jQuery 1.4 has a built in method to handle delays for animations you can do something like this:

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

And then later when you want to clean the animation queue you can do:

$("#mini").stop(true);
呆头 2024-09-03 12:35:48

这将在函数运行延迟 500ms 后清除超时

var timeout = setTimeout(function(){

/* YOUR FUNCTION */

}, 500, function(){
   clearTimeout(timeout);
});

This will clear timeout after function run with delay 500ms

var timeout = setTimeout(function(){

/* YOUR FUNCTION */

}, 500, function(){
   clearTimeout(timeout);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文