-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathonDelay.js
55 lines (45 loc) · 1.53 KB
/
onDelay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* onDelay: THIS FILE IS STILL A WORK IN PROGRESS.
See: http://github.com/bgrins/bindWithDelay for current version
*/
(function($) {
$.fn.onDelay = function(events, selector, data, handler, timeout, throttle) {
// (evt, handler, timeout)
if ($.isFunction(selector)) {
throttle = handler;
timeout = data;
handler = selector;
data = undefined;
selector = undefined;
}
// (evt, selector, handler, timeout) OR (evt, data, handler, timeout)
else if ($.isFunction(data)) {
throttle = timeout;
timeout = handler;
handler = data;
data = undefined;
// (evt, data, handler, timeout)
if ( typeof selector !== "string" ) {
data = selector;
selector = undefined;
}
}
// Allow delayed function to be removed with handler in unbind function
handler.guid = handler.guid || ($.guid && $.guid++);
// Bind each separately so that each element has its own delay
return this.each(function() {
var wait = null;
function callback() {
var event = $.extend(true, { }, arguments[0]);
var that = this;
var throttler = function() {
wait = null;
handler.apply(that, [event]);
};
if (!throttle) { clearTimeout(wait); wait = null; }
if (!wait) { wait = setTimeout(throttler, timeout); }
}
callback.guid = handler.guid;
$(this).on(events, selector, data, callback);
});
};
})(jQuery);