Skip to content

Adding TimingFunctions/IntervalTimer #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
93 changes: 93 additions & 0 deletions TimingFunctions/IntervalTimer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* @author Nandan V
* Sunday, 26 July 2020 8:33 AM
* @description Singleton class that handles the <b>timing of tests</b> and
* specs. <br/> The class is singleton as <u>javascript does not support
* multiple timer instances<u/>.
*/
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()