From ce69fe702d399fa83bf5ad63ef1b1d5a2ee25e2f Mon Sep 17 00:00:00 2001 From: Nandan V Date: Sat, 1 Aug 2020 08:46:55 +0530 Subject: [PATCH 1/3] IntervalTimer added | DIRECTORY.md modified. --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 7120c868ea..11c6e4f418 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -113,6 +113,9 @@ * [ReverseString](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseString.js) * [ReverseWords](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseWords.js) +## Timing Functions + * [IntervalTimer](https://github.com/TheAlgorithms/Javascript/blob/master/TimingFunctions/IntervalTimer.js) + ## Trees * [DepthFirstSearch](https://github.com/TheAlgorithms/Javascript/blob/master/Trees/DepthFirstSearch.js) From fdc0b34b1f8f0752c8a870c7769109fb9e7a5d7a Mon Sep 17 00:00:00 2001 From: Nandan V Date: Sat, 1 Aug 2020 08:48:27 +0530 Subject: [PATCH 2/3] IntervalTimer added | DIRECTORY.md modified. --- TimingFunctions/IntervalTimer.js | 93 ++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 TimingFunctions/IntervalTimer.js diff --git a/TimingFunctions/IntervalTimer.js b/TimingFunctions/IntervalTimer.js new file mode 100644 index 0000000000..14e3bcbaa3 --- /dev/null +++ b/TimingFunctions/IntervalTimer.js @@ -0,0 +1,93 @@ +/** + * @author Nandan V + * Sunday, 26 July 2020 8:33 AM + * @description Singleton class that handles the timing of tests and + * specs.
The class is singleton as javascript does not support + * multiple timer instances. + */ +class IntervalTimer { + /** + * @description Constructor for Timer. + * @param interval Sets the interval for running the timer. + * @param callBack The callback function to be executed. + * @return {IntervalTimer} If exists, the existing object. + */ + constructor (interval = 10, + callBack = () => {}) { + this.prevInterval = 0 + if (this.instance == null) { + this.interval = interval + this.callBack = callBack + this.instance = this + } else { + return this.instance + } + } + + /** + * @description Starts the timer. + */ + startTimer () { + this.timer = setInterval(this.callBack, this.interval) + } + + /** + * @description Resets the timer. + * @return {number} Elapsed time in milliseconds. + */ + resetTimer () { + clearInterval(this.timer) + this.callBack = () => {}; + return this.getElapsedTime() + } + + /** + * @return {number} Elapsed time in milliseconds since reset. + */ + getElapsedTime (offset = 0) { + this.timeElapsed = this.timer - this.prevInterval + this.prevInterval = this.timer + return this.timeElapsed - offset + } + + /** + * @return {number} Elapsed time since start. + */ + getRunTime () { + return this.timer + } +} + +/** + * @author Nandan V + * Saturday, 01 August 2020 8:33 AM + * @description Example usage + */ +const ExampleIntervalTimer = function () { + /** + * Create am object with default settings. + * @type {IntervalTimer} Used to get timing information. + */ + const timer = new IntervalTimer() + timer.startTimer() + + // ... Initialization code ... + // I generally use it for timing tests in Jasmine JS. + + /** + * Gets the runtime till this point. + * Can be subtracted from ElapsedTime to offset timing of initialization code. + */ + const initOffset = timer.getRunTime() + + // ... A test ... + // The time taken to run the test. + console.log(timer.getElapsedTime(initOffset)) + + /** + * Returns the elapsed time and resets the timer to 0. + */ + console.log(timer.resetTimer()) +} + +ExampleIntervalTimer() From 6be5c1d9d33b5b664fa44c8b3cad8dbde14aa25d Mon Sep 17 00:00:00 2001 From: Nandan V Date: Sat, 1 Aug 2020 09:10:41 +0530 Subject: [PATCH 3/3] Fixed "extra ;" issue --- TimingFunctions/IntervalTimer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TimingFunctions/IntervalTimer.js b/TimingFunctions/IntervalTimer.js index 14e3bcbaa3..2864a32c27 100644 --- a/TimingFunctions/IntervalTimer.js +++ b/TimingFunctions/IntervalTimer.js @@ -37,7 +37,7 @@ class IntervalTimer { */ resetTimer () { clearInterval(this.timer) - this.callBack = () => {}; + this.callBack = () => {} return this.getElapsedTime() }