@kevinwin/

debounce

Nodejs

No description

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
function debounce(fn, wait, immediate = false) {
    let timeout;
    
    return function(...args) {
        clearTimeout(timeout);
        const callNow = immediate && !timeout;
        timeout = setTimeout(() => { 
          timeout = null;
          if (!immediate) fn(args)
        }, wait);
		    if (callNow) fn(args);
    }
}
node v10.16.0