From bba5b1e653ba8fae53b577166c744f21ac3c84e2 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Tue, 1 Sep 2015 11:30:55 -0700 Subject: [PATCH] Add 'finish' to time-based helpers --- lib/utils.js | 11 +++++++++++ spec/lib/utils.spec.js | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/utils.js b/lib/utils.js index 4f5ba22..277a1ae 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -42,6 +42,17 @@ var Utils = module.exports = { return every(0, action); }, + /** + * A wrapper around clearInterval + * + * @param {intervalID} action function to invoke constantly + * @example stop(blinking); + * @return {void} + */ + finish: function finish(intervalID) { + clearInterval(intervalID); + }, + /** * Sleeps the program for a period of time. * diff --git a/spec/lib/utils.spec.js b/spec/lib/utils.spec.js index cbd511c..6ef1381 100644 --- a/spec/lib/utils.spec.js +++ b/spec/lib/utils.spec.js @@ -71,6 +71,25 @@ describe("Utils", function() { }); }); + describe("#finish", function() { + beforeEach(function() { + this.clock = sinon.useFakeTimers(); + }); + + afterEach(function() { + this.clock.restore(); + }); + + it("stops calling an interval function", function() { + var func = spy(); + var interval = utils.every(10, func); + this.clock.tick(15); + utils.finish(interval); + this.clock.tick(15); + expect(func).to.be.calledOnce; + }); + }); + describe("#subclass", function() { var BaseClass = function BaseClass(opts) { this.greeting = opts.greeting;