/
throttler.js
57 lines (54 loc) · 2.1 KB
/
throttler.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
56
57
define([], function() {
var throttle = function(cb, wait, thisObj) {
// summary:
// Create a function that will only execute once per `wait` periods.
// description:
// Create a function that will only execute once per `wait` periods
// from last execution when called repeatedly. Useful for preventing excessive
// calculations in rapidly firing events, such as window.resize, node.mousemove
// and so on.
// cb: Function
// The callback to fire.
// wait: Integer
// time to delay before allowing cb to call again.
// thisObj: Object?
// Optional execution context
var canrun = true;
return function() {
if (!canrun) return;
canrun = false;
cb.apply(thisObj || cb, arguments);
setTimeout(function() {
canrun = true;
}, wait);
}
}
var debounce = function(cb, wait, thisObj) {
// 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.
// thisObj: Object?
// Optional execution context.
var timer;
return function() {
if (timer) clearTimeout(timer);
var a = arguments;
timer = setTimeout(function() {
cb.apply(thisObj || cb, a);
}, wait);
}
};
var throttler = {
throttle: throttle,
debounce: debounce
}
return throttler;
})