12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- module.exports = function ( delay, noTrailing, callback, debounceMode ) {
-
-
-
- var timeoutID;
-
- var lastExec = 0;
-
- if ( typeof noTrailing !== 'boolean' ) {
- debounceMode = callback;
- callback = noTrailing;
- noTrailing = undefined;
- }
-
-
-
- function wrapper () {
- var self = this;
- var elapsed = Number(new Date()) - lastExec;
- var args = arguments;
-
- function exec () {
- lastExec = Number(new Date());
- callback.apply(self, args);
- }
-
-
- function clear () {
- timeoutID = undefined;
- }
- if ( debounceMode && !timeoutID ) {
-
-
- exec();
- }
-
- if ( timeoutID ) {
- clearTimeout(timeoutID);
- }
- if ( debounceMode === undefined && elapsed > delay ) {
-
-
- exec();
- } else if ( noTrailing !== true ) {
-
-
-
-
-
-
-
-
-
- timeoutID = setTimeout(debounceMode ? clear : exec, debounceMode === undefined ? delay - elapsed : delay);
- }
- }
-
- return wrapper;
- };
|