1
2// This is from David Walshes Blog Post
3// https://davidwalsh.name/javascript-debounce-function
4// A very simplified but effective version of Underscores debounce function
5function debounce(func, wait, immediate) {
6 var timeout;
7 return function() {
8 var context = this, args = arguments;
9 var later = function() {
10 timeout = null;
11 if (!immediate) func.apply(context, args);
12 };
13 var callNow = immediate && !timeout;
14 clearTimeout(timeout);
15 timeout = setTimeout(later, wait);
16 if (callNow) func.apply(context, args);
17 };
18};
1const debounce = (func, delay) => {
2 let clearTimer;
3 return function() {
4 const context = this;
5 const args = arguments;
6 clearTimeout(clearTimer);
7 clearTimer = setTimeout(() => func.apply(context, args), delay);
8 }
9}