Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canceling a Deferred Promise in jQuery

How can I cancel a promise without removing the element from the 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.

like image 271
ripper234 Avatar asked Mar 07 '12 23:03

ripper234


People also ask

What is Deferred resolve in jQuery?

This deferred. resolve() method in JQuery is used to resolve a Deferred object and call any doneCallbacks with the given arguments. Syntax: deferred.resolve([args]) Parameters: args: This is optional parameters and is arguments which are passed to the doneCallbacks.

What is Deferred and Promise in jQuery?

version added: 1.5deferred.The deferred. promise() method allows an asynchronous function to prevent other code from interfering with the progress or status of its internal request.

Does jQuery support Promise?

jQuery objects can now return a Promise to observe when all animations on a collection have completed.” jQuery provides several methods that support custom animations (for example, the animate(), fadeIn(), and slideUp() methods). Their return value is the jQuery object.

What is Deferred in Javascript?

The defer is a Boolean value, used to indicate that script is executed after the document has been parsed. It works only with external scripts (i.e., works only when we are specifying the src attribute in <script> tag).


2 Answers

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().

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.

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.

like image 185
peter Avatar answered Oct 01 '22 04:10

peter


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

From the jQuery documentation here: 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."

like image 42
Paul Avatar answered Oct 01 '22 03:10

Paul