Skip to content

Instantly share code, notes, and snippets.

@sturob
Created June 26, 2013 14:26
Show Gist options
  • Select an option

  • Save sturob/5867786 to your computer and use it in GitHub Desktop.

Select an option

Save sturob/5867786 to your computer and use it in GitHub Desktop.
Timeouts using jquery promises
$.wait = function(time) {
var id;
var def = $.Deferred(function(dfd) {
id = setTimeout(dfd.resolve, time);
});
def.cancel = function() {
clearTimeout(id);
def.reject();
}
return def;
};
$.wait( 1000 ).then( function() { alert('will display after 1 second') } );
var wtf = $.wait( 2000 ).then( function(){ alert("won't display") } );
wtf.cancel();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment