-
Notifications
You must be signed in to change notification settings - Fork 0
/
commonCode.js
51 lines (44 loc) · 1.1 KB
/
commonCode.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
function debounce(func, wait) {
console.log("Debounce called.");
var timeout = null;
return function() {
if (timeout) clearTimeout(timeout);
timeout = setTimeout(func, wait); // Scheduling
};
}
/**
* Throttle with no leading edge, but with trailing edge.
* @param {*} func The function to throttle.
* @param {*} wait The number of milliseconds to throttle invocations to.
*/
function throttle1(func, wait) {
console.log("Throttle1 called.");
var timeout = null;
return function() {
if (!timeout) {
// Schedule invoking the function.
timeout = setTimeout(function() {
func();
timeout = null;
}, wait);
}
};
}
/**
* Throttle with leading edge, but no trailing edge.
* @param {*} func The function to throttle.
* @param {*} wait The number of milliseconds to throttle invocations to.
*/
function throttle2(func, wait) {
console.log("throttle2 called.");
var isWaiting = false;
return function() {
if (!isWaiting) {
func();
isWaiting = true;
setTimeout(function() {
isWaiting = false;
}, wait);
}
};
}