vn-verdnaturachat/app/utils/debounce.js

21 lines
473 B
JavaScript
Raw Normal View History

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