12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- define(['./restArguments', './now'], function (restArguments, now) {
-
-
-
-
- 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);
-
- 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;
- }
- return debounce;
- });
|