scinote-web/app/javascript/vue/shared/debounce.js

13 lines
332 B
JavaScript
Raw Normal View History

2023-05-17 18:00:37 +08:00
export function debounce(fn, wait){
let timer;
return function(...args){
if(timer) {
clearTimeout(timer); // clear any pre-existing timer
}
const context = this; // get the current context
timer = setTimeout(()=>{
fn.apply(context, args); // call the function if time expires
}, wait);
}
}