* Debounce a function so it only gets called after the * input stops arriving after the given wait period. * * @param {Function} func * @param {Number} wait * @return {Function} - the debounced function
(func, wait)
| 320 | */ |
| 321 | |
| 322 | function _debounce(func, wait) { |
| 323 | var timeout, args, context, timestamp, result; |
| 324 | var later = function later() { |
| 325 | var last = Date.now() - timestamp; |
| 326 | if (last < wait && last >= 0) { |
| 327 | timeout = setTimeout(later, wait - last); |
| 328 | } else { |
| 329 | timeout = null; |
| 330 | result = func.apply(context, args); |
| 331 | if (!timeout) context = args = null; |
| 332 | } |
| 333 | }; |
| 334 | return function () { |
| 335 | context = this; |
| 336 | args = arguments; |
| 337 | timestamp = Date.now(); |
| 338 | if (!timeout) { |
| 339 | timeout = setTimeout(later, wait); |
| 340 | } |
| 341 | return result; |
| 342 | }; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Manual indexOf because it's slightly faster than |