Skip to content

Instantly share code, notes, and snippets.

@danielt69
Created July 24, 2016 05:54
Show Gist options
  • Select an option

  • Save danielt69/de8b89b77e885d9c49e112e86d856f7c to your computer and use it in GitHub Desktop.

Select an option

Save danielt69/de8b89b77e885d9c49e112e86d856f7c to your computer and use it in GitHub Desktop.
Wrap setTimeout in a Deferred
$.wait = function(delay, context){
var deferred = $.Deferred();
var timer = setTimeout(function(){
deferred.resolveWith(context || deferred);
}, delay);
deferred.fail(function(){
clearTimeout(timer);
});
return deferred;
};
// how to use:
$.wait(1250).then(function(){
// your code goes here
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment