Comparing version 1.0.2 to 1.0.4
12
index.js
@@ -27,4 +27,10 @@ module.exports = stifle; | ||
wrapper.cancel = function () { | ||
if (timer) clearTimeout(timer) | ||
timer = undefined | ||
// Clear the called flag, or it would fire twice when called again later | ||
called = false; | ||
// Turn off the timer, so it won't fire after the wait expires | ||
if (timer) { | ||
clearTimeout(timer); | ||
timer = undefined; | ||
} | ||
} | ||
@@ -39,3 +45,3 @@ | ||
called = false; | ||
wrapper() | ||
wrapper(); | ||
} | ||
@@ -42,0 +48,0 @@ } |
{ | ||
"name": "stifle", | ||
"version": "1.0.2", | ||
"version": "1.0.4", | ||
"description": "A very simple way to throttle a function", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -23,3 +23,3 @@ stifle | ||
The wrapped function comes with a `cancel` method to kill and pending future invocations -- useful for shutting it down when a page or component is being unloaded. | ||
The wrapped function comes with a `cancel` method to kill any pending future invocations -- useful for shutting it down when a page or component is being unloaded. | ||
@@ -26,0 +26,0 @@ |
@@ -39,2 +39,19 @@ var stifle = require('../index') | ||
it('should not trigger an extra call after a `cancel`', function (done) { | ||
var callCount = 0, | ||
wrapper = stifle(function () { callCount++; }, 10); | ||
wrapper(); | ||
wrapper(); // This call should get cancelled | ||
wrapper.cancel(); | ||
assert.strictEqual(callCount, 1); | ||
wrapper(); | ||
setTimeout(function () { | ||
assert.strictEqual(callCount, 2); | ||
done(); | ||
}, 50); | ||
}); | ||
it('should trigger a second wrapper call as soon as possible', function (done) { | ||
@@ -41,0 +58,0 @@ var callCount = 0; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15343
171