jQuery helper for setTimeout

This technique was lifted directly from Alex McPherson's jQuery Conference 2012 presentation entitled "I .promise() to show you .when() to use Deferreds". The whole talk is great but I love this little snippet and had to share:

1
2
3
4
5
$.wait = function(duration) {
   return $.Deferred(function(def) {
        setTimeout(def.resolve, duration);
   })
}

Usage:

1
$.wait(1000).then(goMakeTea);

The slides are available on GitHub.