-
Notifications
You must be signed in to change notification settings - Fork 122
/
throttle.js
63 lines (59 loc) · 1.64 KB
/
throttle.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
58
59
60
61
62
63
// Throttling is a technique in which,
// no matter how many times the user fires the event,
// the attached function will be executed only once in a given time interval.
/**
* @param {Function} fn
* @param {number} t milliseconds
* @return {Function}
*/
const throttle = function (fn, t) {
let timer;
return (...args) => {
const context = this;
if (timer) {
return;
}
timer = setTimeout(() => {
fn.apply(context, args);
timer = undefined;
}, t);
};
};
const throttleImmediate = function (fn, t, immediate = false) {
let timer;
return (...args) => {
const context = this;
const callNow = immediate && !timer;
if (!timer) {
timer = setTimeout(() => {
if (!immediate) {
fn.apply(context, args);
}
timer = undefined;
}, t);
}
if (callNow) {
fn.apply(context, args);
}
};
};
const imitateCalls = function (fn) {
let i = 0;
fn(`Hello${++i}`);
const timer = setInterval(function () {
if (i >= 9) {
clearInterval(timer);
}
fn(`Hello${++i}`);
}, 100);
};
const start = Date.now();
imitateCalls(throttle((str) => {
console.log('Case throttle:', str, Date.now() - start);
}, 500)); // Hello1 logged at t = ~500ms, Hello6 logged at t = ~1000ms
imitateCalls(throttleImmediate((str) => {
console.log('Case throttle immediate - false:', str, Date.now() - start);
}, 500)); // Hello1 logged at t = ~500ms, Hello6 logged at t = ~1000ms
imitateCalls(throttleImmediate((str) => {
console.log('Case throttle immediate - true:', str, Date.now() - start);
}, 500, true)); // Hello1 logged at t = ~0ms, Hello6 logged at t = ~500ms