Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1841d1f

Browse files
Adding TimingFunctions/IntervalTimer (TheAlgorithms#237)
* IntervalTimer added | DIRECTORY.md modified.* IntervalTimer added | DIRECTORY.md modified.* Fixed "extra ;" issue
1 parente1d546e commit1841d1f

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

‎DIRECTORY.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@
113113
*[ReverseString](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseString.js)
114114
*[ReverseWords](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseWords.js)
115115

116+
##Timing Functions
117+
*[IntervalTimer](https://github.com/TheAlgorithms/Javascript/blob/master/TimingFunctions/IntervalTimer.js)
118+
116119
##Trees
117120
*[DepthFirstSearch](https://github.com/TheAlgorithms/Javascript/blob/master/Trees/DepthFirstSearch.js)
118121

‎TimingFunctions/IntervalTimer.js‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
*@author Nandan V
3+
* Sunday, 26 July 2020 8:33 AM
4+
*@description Singleton class that handles the <b>timing of tests</b> and
5+
* specs. <br/> The class is singleton as <u>javascript does not support
6+
* multiple timer instances<u/>.
7+
*/
8+
classIntervalTimer{
9+
/**
10+
*@description Constructor for Timer.
11+
*@param interval Sets the interval for running the timer.
12+
*@param callBack The callback function to be executed.
13+
*@return {IntervalTimer} If exists, the existing object.
14+
*/
15+
constructor(interval=10,
16+
callBack=()=>{}){
17+
this.prevInterval=0
18+
if(this.instance==null){
19+
this.interval=interval
20+
this.callBack=callBack
21+
this.instance=this
22+
}else{
23+
returnthis.instance
24+
}
25+
}
26+
27+
/**
28+
*@description Starts the timer.
29+
*/
30+
startTimer(){
31+
this.timer=setInterval(this.callBack,this.interval)
32+
}
33+
34+
/**
35+
*@description Resets the timer.
36+
*@return {number} Elapsed time in milliseconds.
37+
*/
38+
resetTimer(){
39+
clearInterval(this.timer)
40+
this.callBack=()=>{}
41+
returnthis.getElapsedTime()
42+
}
43+
44+
/**
45+
*@return {number} Elapsed time in milliseconds since reset.
46+
*/
47+
getElapsedTime(offset=0){
48+
this.timeElapsed=this.timer-this.prevInterval
49+
this.prevInterval=this.timer
50+
returnthis.timeElapsed-offset
51+
}
52+
53+
/**
54+
*@return {number} Elapsed time since start.
55+
*/
56+
getRunTime(){
57+
returnthis.timer
58+
}
59+
}
60+
61+
/**
62+
*@author Nandan V
63+
* Saturday, 01 August 2020 8:33 AM
64+
*@description Example usage
65+
*/
66+
constExampleIntervalTimer=function(){
67+
/**
68+
* Create am object with default settings.
69+
*@type {IntervalTimer} Used to get timing information.
70+
*/
71+
consttimer=newIntervalTimer()
72+
timer.startTimer()
73+
74+
// ... Initialization code ...
75+
// I generally use it for timing tests in Jasmine JS.
76+
77+
/**
78+
* Gets the runtime till this point.
79+
* Can be subtracted from ElapsedTime to offset timing of initialization code.
80+
*/
81+
constinitOffset=timer.getRunTime()
82+
83+
// ... A test ...
84+
// The time taken to run the test.
85+
console.log(timer.getElapsedTime(initOffset))
86+
87+
/**
88+
* Returns the elapsed time and resets the timer to 0.
89+
*/
90+
console.log(timer.resetTimer())
91+
}
92+
93+
ExampleIntervalTimer()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp