Added a simply debounce util to avoid using throttle plugins
This commit is contained in:
15
assets/scripts/utils/debounce.js
Normal file
15
assets/scripts/utils/debounce.js
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
export default function(func, wait, immediate) {
|
||||||
|
let timeout;
|
||||||
|
return function() {
|
||||||
|
const context = this;
|
||||||
|
const args = arguments;
|
||||||
|
const later = function() {
|
||||||
|
timeout = null;
|
||||||
|
if (!immediate) func.apply(context, args);
|
||||||
|
};
|
||||||
|
const callNow = immediate && !timeout;
|
||||||
|
clearTimeout(timeout);
|
||||||
|
timeout = setTimeout(later, wait);
|
||||||
|
if (callNow) func.apply(context, args);
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user