Skip to content

Commit

Permalink
Ensure _.throttle clears the timeout when func is called. [closes #…
Browse files Browse the repository at this point in the history
…86]

Former-commit-id: 6f3b777aa247f059d97f965c02323d4ee6ab8464
  • Loading branch information
jdalton committed Oct 9, 2012
1 parent db3b429 commit fd79056
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions lodash.js
Expand Up @@ -3468,6 +3468,7 @@
thisArg = this;

if (remain <= 0) {
clearTimeout(timeoutId);
lastCalled = now;
result = func.apply(thisArg, args);
}
Expand Down
25 changes: 25 additions & 0 deletions test/test.js
Expand Up @@ -1597,6 +1597,31 @@

throttled();
});

asyncTest('should clear timeout when `func` is called', function() {
var now = new Date,
times = [];

var throttled = _.throttle(function() {
times.push(new Date - now);
}, 20);

setTimeout(throttled, 20);
setTimeout(throttled, 20);
setTimeout(throttled, 40);
setTimeout(throttled, 40);

setTimeout(function() {
var actual = _.every(times, function(value, index) {
return index
? (value - times[index - 1]) > 15
: true;
});

ok(actual);
QUnit.start();
}, 120);
});
}());

/*--------------------------------------------------------------------------*/
Expand Down

0 comments on commit fd79056

Please sign in to comment.