How can I cancel a promise without removing the element from the DOM?
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.
Good news. Since yesterday you can cancel your promise.
I published the new version of my small plugin jquery-timing that provides two methods amongst many others called .wait() and .unwait().
If you then want to unregister the callback:
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:
or the whole code in one chain, without unwait option:
or the same even shorter with option to cancel the two pauses:
Just see the docs, examples, and API what fits best for you.
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).
I believe you can use
$('#box').remove();
From the jQuery documentation here: http://api.jquery.com/promise/
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/
As a side note, deferreds can only rejected or resolved once. Once they are rejected or resolved, you cannot change their state.