Skip to content

Instantly share code, notes, and snippets.

@hlfcoding
Created February 5, 2015 10:47
Show Gist options
  • Save hlfcoding/2fbb817f35e4b8216746 to your computer and use it in GitHub Desktop.
Save hlfcoding/2fbb817f35e4b8216746 to your computer and use it in GitHub Desktop.
jQuery.fn.wait
// jQuery.fn.wait
// Copyright 2015 pengxwang.com
// Released under the MIT license
/*
$.fn.wait = (args...) ->
d = $.Deferred()
k = "wait-#{id}-promise"
p = d.promise()
if args.length is 1
[id] = args
d.resolve() and @.data(k, p) if not @.data(k)?
@.data(k)
else
[ms, id] = args
setTimeout d.resolve.bind(d), ms
@.data "wait-#{id}-promise", p
p
*/
var __slice = [].slice;
$.fn.wait = function() {
var args, d, id, k, ms, p;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
d = $.Deferred();
k = "wait-" + id + "-promise";
p = d.promise();
if (args.length === 1) {
id = args[0];
if (this.data(k) == null) {
d.resolve() && this.data(k, p);
}
return this.data(k);
} else {
ms = args[0], id = args[1];
setTimeout(d.resolve.bind(d), ms);
this.data("wait-" + id + "-promise", p);
return p;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment