2017-06-13 18:40:05 -06:00
|
|
|
export default function debounce(func, wait, immediate) {
|
2019-10-07 17:11:53 -06:00
|
|
|
let timeout;
|
|
|
|
return function () {
|
|
|
|
const context = this;
|
|
|
|
const args = arguments;
|
|
|
|
const later = function () {
|
2017-12-12 19:27:20 -07:00
|
|
|
timeout = null;
|
|
|
|
if (!immediate) func.apply(context, args);
|
2017-06-13 18:40:05 -06:00
|
|
|
};
|
2019-10-07 17:11:53 -06:00
|
|
|
const callNow = immediate && !timeout;
|
2017-12-12 19:27:20 -07:00
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(later, wait);
|
|
|
|
if (callNow) func.apply(context, args);
|
|
|
|
};
|
|
|
|
}
|