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;