| import restArguments from './restArguments.js'; | |
| import now from './now.js'; | |
| // When a sequence of calls of the returned function ends, the argument | |
| // function is triggered. The end of a sequence is defined by the `wait` | |
| // parameter. If `immediate` is passed, the argument function will be | |
| // triggered at the beginning of the sequence instead of at the end. | |
| export default function debounce(func, wait, immediate) { | |
| var timeout, previous, args, result, context; | |
| var later = function() { | |
| var passed = now() - previous; | |
| if (wait > passed) { | |
| timeout = setTimeout(later, wait - passed); | |
| } else { | |
| timeout = null; | |
| if (!immediate) result = func.apply(context, args); | |
| // This check is needed because `func` can recursively invoke `debounced`. | |
| if (!timeout) args = context = null; | |
| } | |
| }; | |
| var debounced = restArguments(function(_args) { | |
| context = this; | |
| args = _args; | |
| previous = now(); | |
| if (!timeout) { | |
| timeout = setTimeout(later, wait); | |
| if (immediate) result = func.apply(context, args); | |
| } | |
| return result; | |
| }); | |
| debounced.cancel = function() { | |
| clearTimeout(timeout); | |
| timeout = args = context = null; | |
| }; | |
| return debounced; | |
| } | |
Xet Storage Details
- Size:
- 1.22 kB
- Xet hash:
- f836f6ba90a5f948d82bca6669536ea637702cb75e3c3142e6d9a6fc50f7e7fe
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.