-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebounce.js
33 lines (28 loc) · 953 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
31
32
33
/**
* Debounce a callback.
* @param callback The callback to debounce.
* @param buffer The debounce buffer in milliseconds.
* @param immediate Whether to execute the callback at the beginning
* of the interval. If false, the callback will be executed at the end.
* @returns The debounced callback.
*/
export function debounce( callback, buffer, immediate ) {
// Track the timeout ID so that only one callback can be queued at a time.
let timeoutID
return function () {
const shouldCallImmediately = immediate && !timeoutID
// Create a delayed callback.
const delayedCallback = () => {
timeoutID = null
if ( !immediate ) {
callback.apply( this, arguments )
}
}
clearTimeout( timeoutID )
timeoutID = setTimeout( delayedCallback, buffer )
// Call immediately if needed (moved after timeout setup)
if ( shouldCallImmediately ) {
callback.apply( this, arguments )
}
}
}