javascript 取消 jQuery 中的延迟承诺

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9611021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:12:46  来源:igfitidea点击:

Canceling a Deferred Promise in jQuery

javascriptjqueryjquery-deferredpromise

提问by ripper234

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

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

fiddle

小提琴

I ran this code:

我运行了这个代码:

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

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.

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

采纳答案by peter

Good news. Since yesterday you can cancel your promise.

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

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

我发布了我的小插件jquery-timing的新版本,它提供了两种方法,其中包括 .wait() 和 .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();

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

这些静态版本的 wait 和 unwait 还支持可选的组名,以不取消任何处理程序,而只取消一个特定的集合。

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

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

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

回答by Paul

I believe you can use $('#box').remove();

我相信你可以使用 $('#box').remove();

From the jQuery documentation here: http://api.jquery.com/promise/

从这里的 jQuery 文档:http: //api.jquery.com/promise/

The returned Promise is linked to a Deferred object stored on the .data() for an element. Since the .remove() method removes the element's data as well as the element itself, it will prevent any of the element's unresolved Promises from resolving. If it is necessary to remove an element from the DOM before its Promise is resolved, use .detach() instead and follow with .removeData() after resolution."

返回的 Promise 链接到存储在 .data() 元素上的 Deferred 对象。由于 .remove() 方法删除元素的数据以及元素本身,它将阻止任何元素的未解析 Promise 解析。如果需要在解析 Promise 之前从 DOM 中删除元素,请改用 .detach() 并在解析后使用 .removeData() 。”

回答by JayC

I don't suppose you'd want something like http://jsfiddle.net/2cq8M/? I'm involving two promises (one just to handle the case at the end of the set of animations, the other to resolve or reject as needed).

我不认为你想要像http://jsfiddle.net/2cq8M/这样的东西?我涉及两个承诺(一个只是在动画集结束时处理案例,另一个根据需要解决或拒绝)。

回答by Patrick Lee Scott

You want to use a deferred in this case instead of a promise, however, you can use the promise of the animation to resolve the deferred.

在这种情况下,您想使用延迟而不是承诺,但是,您可以使用动画的承诺来解决延迟。

http://jsfiddle.net/LG9eZ/9/

http://jsfiddle.net/LG9eZ/9/

var stopDone = true;

function log(msg) {
    $(".debug").append(new Date() + " - " + msg + "<br/>");
}

log("Starting");

var boxAnimation = new $.Deferred();
boxAnimation.done(function() {
    log("Done");
});
boxAnimation.fail(function() {
    log("Stopped");
});


$("#box").delay(2000).show("slow").delay(2000).promise().then(function() {
    boxAnimation.resolve(); // when all the animations are done, resolve the deferred.
}); 


if (stopDone)
{
    boxAnimation.reject();
}

As a side note, deferreds can only rejected or resolved once. Once they are rejected or resolved, you cannot change their state.

作为旁注,延迟只能拒绝或解决一次。一旦它们被拒绝或解决,您就无法更改它们的状态。