Implement debounce function
This commit is contained in:
parent
c8baff0f48
commit
54edd4b517
22
resources/js/helpers.js
vendored
Normal file
22
resources/js/helpers.js
vendored
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
export function debounce(func, wait, immediate) {
|
||||||
|
let timeout;
|
||||||
|
return function() {
|
||||||
|
let context = this, args = arguments;
|
||||||
|
|
||||||
|
let later = function() {
|
||||||
|
timeout = null;
|
||||||
|
if (!immediate) {
|
||||||
|
func.apply(context, args);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let callNow = immediate && !timeout;
|
||||||
|
|
||||||
|
clearTimeout(timeout);
|
||||||
|
timeout = setTimeout(later, wait);
|
||||||
|
|
||||||
|
if (callNow) {
|
||||||
|
func.apply(context, args);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user