Skip to content

Commit 0636d40

Browse files
more simple debounce
1 parent a48abee commit 0636d40

File tree

3 files changed

+122
-241
lines changed

3 files changed

+122
-241
lines changed

Docs/Types/Function.md

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,12 @@ This method will return a new function that will be called only once per group o
375375

376376
### Syntax:
377377

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

380380
### Arguments:
381381

382-
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.
382+
1. delay - (*number*, optional, defaults to 250ms) The delay to wait before a call to the debounced function can happen again.
383+
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.
383384

384385
### Returns:
385386

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

397-
// send a Request to server with data from a search field
398-
// 500ms after you stopped typing, to avoid one request per character
399-
var input = document.getElement('input');
400-
var request = new Request({
401-
url: 'your.url',
402-
onSuccess: function(response) {
403-
$('res').set('html', response);
404-
}
405-
});
406-
407-
function fn(){
408-
request.send({data: {value: this.value}});
409-
}
410-
411-
input.addEventListener('keyup', fn.debounce(500));
412-
413-
414-
### Options:
415-
416-
* *delay* - The delay after which the debounce function is called. Defaults to 250ms.
417-
* *when* - When to fire the debounce call. Can be at beginning of group call (*early*), at end (*late*) or *both*. Defaults to *late*.
418-
* *once* - If *true* will call the function only once per event handler. Defaults to *false*.
419-
420398

421399

422400
Deprecated Functions {#Deprecated-Functions}

Source/Types/Function.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,32 @@ Function.implement({
7474
return setInterval(this.pass((args == null ? [] : args), bind), periodical);
7575
},
7676

77-
debounce: function(opts){
78-
var timeout,
77+
debounce: function(delay, leading){
78+
79+
// in case delay is omitted and `leading` is first argument
80+
if (typeof delay == 'boolean'){
81+
leading = delay;
82+
delay = false;
83+
}
84+
85+
var timeout, args, self,
7986
fn = this,
80-
fireOnce = 0;
81-
if (typeof opts == 'number') opts = {delay: opts};
82-
else opts = opts || {};
83-
if (!opts.delay) opts.delay = 250;
87+
callNow = leading;
88+
89+
var later = function(){
90+
if (leading) callNow = true;
91+
else fn.apply(self, args);
92+
timeout = null;
93+
};
8494

8595
return function(){
86-
if (fireOnce == 2 && opts.when == 'both' || fireOnce && opts.when != 'both') return;
87-
var self = this,
88-
args = arguments,
89-
callNow = !timeout && (opts.when == 'early' || opts.when == 'both');
90-
var later = function(){
91-
if (opts.when != 'early') fn.apply(self, args);
92-
timeout = null;
93-
if (opts.once) fireOnce = 2;
94-
};
96+
self = this;
97+
args = arguments;
98+
9599
clearTimeout(timeout);
96-
timeout = setTimeout(later, opts.delay);
100+
timeout = setTimeout(later, delay || 250);
97101
if (callNow) fn.apply(self, args);
98-
if (opts.once && callNow) fireOnce = 1;
102+
callNow = false;
99103
};
100104
}
101105

0 commit comments

Comments
 (0)