Skip to content

Commit

Permalink
simplified debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioCrisostomo committed Oct 24, 2015
1 parent a48abee commit cf32346
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 241 deletions.
28 changes: 3 additions & 25 deletions Docs/Types/Function.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,12 @@ This method will return a new function that will be called only once per group o

### Syntax:

var debounceFn = myFn.debounce({delay: 100});
var debounceFn = myFn.debounce(delay, leading);

### Arguments:

1. obj - (*mixed*) If this argument is a number it will use it as the *delay* timeout of the debounce. If this argument is a object it will allow more specific configuration. Leaving the argument empty will fall to default values.
1. delay - (*number*, optional, defaults to 250ms) The delay to wait before a call to the debounced function can happen again.
2. leading - (*boolean*, optional, defaults to false) If the call to the debounced function should happen in leading phase of group of calls or after.

### Returns:

Expand All @@ -394,29 +395,6 @@ This method will return a new function that will be called only once per group o
}
window.addEvent('scroll', getNewScrollPosition.debounce(500));

// send a Request to server with data from a search field
// 500ms after you stopped typing, to avoid one request per character
var input = document.getElement('input');
var request = new Request({
url: 'your.url',
onSuccess: function(response) {
$('res').set('html', response);
}
});

function fn(){
request.send({data: {value: this.value}});
}

input.addEventListener('keyup', fn.debounce(500));


### Options:

* *delay* - The delay after which the debounce function is called. Defaults to 250ms.
* *when* - When to fire the debounce call. Can be at beginning of group call (*early*), at end (*late*) or *both*. Defaults to *late*.
* *once* - If *true* will call the function only once per event handler. Defaults to *false*.



Deprecated Functions {#Deprecated-Functions}
Expand Down
38 changes: 21 additions & 17 deletions Source/Types/Function.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,32 @@ Function.implement({
return setInterval(this.pass((args == null ? [] : args), bind), periodical);
},

debounce: function(opts){
var timeout,
debounce: function(delay, leading){

// in case delay is omitted and `leading` is first argument
if (typeof delay == 'boolean'){
leading = delay;
delay = false;
}

var timeout, args, self,
fn = this,
fireOnce = 0;
if (typeof opts == 'number') opts = {delay: opts};
else opts = opts || {};
if (!opts.delay) opts.delay = 250;
callNow = leading;

var later = function(){
if (leading) callNow = true;
else fn.apply(self, args);
timeout = null;
};

return function(){
if (fireOnce == 2 && opts.when == 'both' || fireOnce && opts.when != 'both') return;
var self = this,
args = arguments,
callNow = !timeout && (opts.when == 'early' || opts.when == 'both');
var later = function(){
if (opts.when != 'early') fn.apply(self, args);
timeout = null;
if (opts.once) fireOnce = 2;
};
self = this;
args = arguments;

clearTimeout(timeout);
timeout = setTimeout(later, opts.delay);
timeout = setTimeout(later, delay || 250);
if (callNow) fn.apply(self, args);
if (opts.once && callNow) fireOnce = 1;
callNow = false;
};
}

Expand Down
Loading

0 comments on commit cf32346

Please sign in to comment.