-
Notifications
You must be signed in to change notification settings - Fork 0
/
debounce.js
30 lines (29 loc) · 903 Bytes
/
debounce.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
define([], function(){
// module:
// dojo/debounce
// summary:
// This module provide a debouncer
return function(cb, wait){
// summary:
// Create a function that will only execute after `wait` milliseconds
// description:
// Create a function that will only execute after `wait` milliseconds
// of repeated execution. Useful for delaying some event action slightly to allow
// for rapidly-firing events such as window.resize, node.mousemove and so on.
// cb: Function
// A callback to fire. Like hitch() and partial(), arguments passed to the
// returned function curry along to the original callback.
// wait: Integer
// Time to spend caching executions before actually executing.
var timer;
return function(){
if(timer){
clearTimeout(timer);
}
var a = arguments;
timer = setTimeout(function(){
cb.apply(this, a);
}, wait);
};
};
});