Ensured _.throttle nulls the timeoutId. [closes #129]

Former-commit-id: 24242f513e01adb2827cc3a5af6c8904098a9280
This commit is contained in:
John-David Dalton
2012-12-03 01:04:54 -08:00
parent 351b2b320e
commit 9bccc9c53c
6 changed files with 65 additions and 40 deletions

View File

@@ -1742,24 +1742,12 @@
(function() {
test('subsequent calls should return the result of the first call', function() {
var throttled = _.throttle(function(value) { return value; }, 90),
var throttled = _.throttle(function(value) { return value; }, 32),
result = [throttled('x'), throttled('y')];
deepEqual(result, ['x', 'x']);
});
test('supports calls in a loop', function() {
var counter = 0,
throttled = _.throttle(function() { counter++; }, 90),
start = new Date,
limit = 180;
while ((new Date - start) < limit) {
throttled();
}
ok(counter > 1);
});
asyncTest('supports recursive calls', function() {
var counter = 0;
var throttled = _.throttle(function() {
@@ -1801,6 +1789,41 @@
QUnit.start();
}, 260);
});
asyncTest('should not trigger a trailing call when invoked once', function() {
var counter = 0,
throttled = _.throttle(function() { counter++; }, 32);
throttled();
equal(counter, 1);
setTimeout(function() {
equal(counter, 1);
QUnit.start();
}, 64);
});
asyncTest('should trigger a trailing call when invoked in a loop', function() {
var counter = 0,
limit = 90,
throttled = _.throttle(function() { counter++; }, 32),
start = new Date;
while ((new Date - start) < limit) {
throttled();
}
setTimeout(function() {
throttled();
throttled();
}, 64);
setTimeout(function() {
ok(counter > 4);
QUnit.start();
}, 96);
ok(counter > 1);
});
}());
/*--------------------------------------------------------------------------*/