在jQuery中取消延迟的Promise [英] Canceling a Deferred Promise in jQuery
问题描述
如何在不从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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!