Skip to content

Instantly share code, notes, and snippets.

@iknite
Last active December 10, 2015 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iknite/4434913 to your computer and use it in GitHub Desktop.
Save iknite/4434913 to your computer and use it in GitHub Desktop.
Delay based in jQuery Deferred object.
(function (window, scope, undefined){
"use strict";
var $ = window.jQuery;
scope.delay = function (timeout){
var $d = $.Deferred(),
t = timeout || 0;
setTimeout(function (){
$d.resolve(timeout)
}, t);
return $d.promise();
};
}(window, scope || window));
@iknite
Copy link
Author

iknite commented Jan 2, 2013

Usage:

    function callback(t) {
      // do stuff here 
      /*you can return a new instance to make a new step, 
      also can update the sleep time or use the previous as shown
      */
      return delay(t);
    }

    delay(timeout).then(callback).then(callback).then(callback); // example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment