Skip to content

Commit

Permalink
Fixes jashkenas#1186, throttle with leading=false, let it expire, the…
Browse files Browse the repository at this point in the history
…n call again expecting leading=false to kick in again. questionable semantics.
  • Loading branch information
jashkenas committed Jul 6, 2013
1 parent d2a3f08 commit 7c658b2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions test/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,27 @@ $(document).ready(function() {
}, 96);
});

asyncTest("more throttle does not trigger leading call when leading is set to false", 3, function() {
var counter = 0;
var incr = function(){ counter++; };
var throttledIncr = _.throttle(incr, 100, {leading: false});

throttledIncr();
_.delay(throttledIncr, 50);
_.delay(throttledIncr, 60);
_.delay(throttledIncr, 200);
ok(counter === 0);

_.delay(function() {
ok(counter == 1);
}, 250);

_.delay(function() {
ok(counter == 2);
start();
}, 350);
});

asyncTest("throttle does not trigger trailing call when trailing is set to false", 4, 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 @@ -668,7 +668,7 @@
};
return function() {
var now = new Date;
if (!previous && options.leading === false) previous = now;
if (options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
Expand Down

0 comments on commit 7c658b2

Please sign in to comment.