Skip to content

Commit

Permalink
Make _.throttle reentrant.
Browse files Browse the repository at this point in the history
  • Loading branch information
int3 committed Apr 29, 2012
1 parent c9ab76e commit 6e5d5b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions test/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ $(document).ready(function() {
_.delay(function(){ equal(counter, 2, "incr was called twice"); start(); }, 220);
});

asyncTest("functions: throttle recursively", 1, function() {
var counter = 0;
var incr = _.throttle(function() {
counter++;
if (counter < 5) incr();
}, 100);
incr();
_.delay(function(){ equal(counter, 3, "incr was throttled"); start(); }, 220);
});

asyncTest("functions: debounce", 1, function() {
var counter = 0;
var incr = function(){ counter++; };
Expand Down
2 changes: 1 addition & 1 deletion underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,10 @@
if (throttling) {
more = true;
} else {
throttling = true;
result = func.apply(context, args);
}
whenDone();
throttling = true;
return result;
};
};
Expand Down

0 comments on commit 6e5d5b8

Please sign in to comment.