diff --git a/lodash.js b/lodash.js index a69543cae..3a6edb81a 100644 --- a/lodash.js +++ b/lodash.js @@ -3468,6 +3468,7 @@ thisArg = this; if (remain <= 0) { + clearTimeout(timeoutId); lastCalled = now; result = func.apply(thisArg, args); } diff --git a/test/test.js b/test/test.js index a6d102219..a5c5201c4 100644 --- a/test/test.js +++ b/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); + }); }()); /*--------------------------------------------------------------------------*/