-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.js
More file actions
44 lines (37 loc) · 1.21 KB
/
function.js
File metadata and controls
44 lines (37 loc) · 1.21 KB
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
/*
------------------------------------------------------------------------------
Throttles execution of a function.
Borrowed from: https://medium.com/@_jh3y/throttling-and-debouncing-in-javascript-b01cad5c8edf
@param {Integer} delay Miliseconds between function calls
@param {Function} fn The function to call
*/
const throttle = (delay, fn) => {
let inThrottle
return function (...args) {
const context = this
if (!inThrottle) {
fn.apply(context, args)
inThrottle = true
setTimeout(() => (inThrottle = false), delay)
}
}
}
/*
------------------------------------------------------------------------------
Debounces execution of a function.
Borrowed from: https://medium.com/@_jh3y/throttling-and-debouncing-in-javascript-b01cad5c8edf
@param {Integer} delay Miliseconds after when the function is called
@param {Function} fn The function to call
*/
const debounce = (delay, fn) => {
let inDebounce
return function (...args) {
const context = this
clearTimeout(inDebounce)
inDebounce = setTimeout(() => fn.apply(context, args), delay)
}
}
/*
------------------------------------------------------------------------------
*/
export { throttle, debounce }