* Returns a function, that, as long as it continues to be invoked, will not * be triggered. The function will be called after it stops being called for * N milliseconds. * * @param func The function to be debounced * @param wait The time in ms to debounce
(func, wait)
| 73 | * @param wait The time in ms to debounce |
| 74 | */ |
| 75 | function debounce(func, wait) { |
| 76 | var timeout; |
| 77 | |
| 78 | return function() { |
| 79 | var context = this, args = arguments; |
| 80 | var later = function() { |
| 81 | timeout = null; |
| 82 | func.apply(context, args); |
| 83 | }; |
| 84 | |
| 85 | clearTimeout(timeout); |
| 86 | timeout = setTimeout(later, wait); |
| 87 | if (!timeout) func.apply(context, args); |
| 88 | }; |
| 89 | }; |