在jQuery中取消延迟的Promise

编程入门 行业动态 更新时间:2024-10-23 06:31:06
本文介绍了在jQuery中取消延迟的Promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在不从DOM中删除元素的情况下取消承诺?

How can I cancel a promise without removing the element from the DOM?

小提琴

我运行了此代码:

$("#box") .delay(2000) .show("slow") .delay(2000) .promise() .then(function(){log("Done");});

在此之后,有没有办法取消承诺? clearQueue()和 stop(true)都不起作用,因为它不是我正在尝试的动画取消。我看到 remove()应该这样做......但我只想停止承诺,而不是删除整个元素。

After this, is there a way to cancel the promise? Both clearQueue() and stop(true) didn't work, because it's not an animation that I'm trying to cancel. I saw that remove() should do it ... but I only want to stop the promise, not remove the entire element.

推荐答案

好消息。从昨天开始,你可以取消你的承诺。

Good news. Since yesterday you can cancel your promise.

我发布了我的小插件的新版本 jquery-timing ,提供两种方法,称为.wait()和.unwait()。

I published the new version of my small plugin jquery-timing that provides two methods amongst many others called .wait() and .unwait().

var deferred = $("#box").delay(2000).show("slow").delay(2000).promise(); $.wait(deferred, function(){ log("Done"); });

如果您想取消注册回调:

If you then want to unregister the callback:

$.unwait();

这些静态版本的wait和unwait也支持一个可选的组名,不能取消任何处理程序,只能取消具体设置。

These static versions of wait and unwait also support an optional group name to not cancel any handler but only a specific set.

除此之外,您还可以做更多智能的事情:

Besides that you can do a lot more smart stuff like:

$('#box').wait(deferred).addClass('ready');

或一个链中的整个代码,没有等待选项:

or the whole code in one chain, without unwait option:

$("#box").delay(2000).show("slow") .delay(2000).join(function(){log("Done");})).addClass('ready');

或更短的选项可以取消两次暂停:

or the same even shorter with option to cancel the two pauses:

$("#box").wait(2000).show("slow",$) .wait(2000, function(){log("Done");})).addClass('ready');

只需查看最适合您的文档,示例和API。

Just see the docs, examples, and API what fits best for you.

更多推荐

在jQuery中取消延迟的Promise

本文发布于:2023-11-10 09:41:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575030.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:jQuery   Promise

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!