21 lines
535 B
JavaScript
21 lines
535 B
JavaScript
|
/**
|
||
|
* Returns a function, that, as long as it continues to be invoked, will not
|
||
|
* be triggered. The function will be called after it stops being called for
|
||
|
* N milliseconds.
|
||
|
* https://stackoverflow.com/a/53486112
|
||
|
*
|
||
|
* @param {Function} fn function to wrap
|
||
|
* @param {number} delay timeout in ms
|
||
|
*/
|
||
|
export function debounce(fn, delay) {
|
||
|
let timeoutID = null
|
||
|
return function() {
|
||
|
clearTimeout(timeoutID)
|
||
|
const args = arguments
|
||
|
const that = this
|
||
|
timeoutID = setTimeout(function() {
|
||
|
fn.apply(that, args)
|
||
|
}, delay)
|
||
|
}
|
||
|
}
|