mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 07:47:49 +00:00
Ensure _.throttle clears the timeout when func is called. [closes #86]
Former-commit-id: 6f3b777aa247f059d97f965c02323d4ee6ab8464
This commit is contained in:
@@ -3468,6 +3468,7 @@
|
||||
thisArg = this;
|
||||
|
||||
if (remain <= 0) {
|
||||
clearTimeout(timeoutId);
|
||||
lastCalled = now;
|
||||
result = func.apply(thisArg, args);
|
||||
}
|
||||
|
||||
25
test/test.js
25
test/test.js
@@ -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);
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
Reference in New Issue
Block a user