vn-verdnaturachat/app/utils/debounce.js

15 lines
403 B
JavaScript
Raw Normal View History

2017-08-11 18:18:09 +00:00
export default function debounce(func, wait, immediate) {
let timeout;
return function _debounce(...args) {
const context = this;
const later = function __debounce() {
timeout = null;
if (!immediate) { func.apply(context, args); }
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) { func.apply(context, args); }
};
}