-
Notifications
You must be signed in to change notification settings - Fork 0
/
STOPWATCH.js
41 lines (38 loc) · 980 Bytes
/
STOPWATCH.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class stopwatch{
constructor()
{
let duration=0,startTime,endTime,flag=0;
this.start=function(){
if (flag==0){
startTime=new Date();
flag=1;
}
else{
console.log('Clock has already started');
}
}
this.stop=function(){
if(flag==1)
{
endTime=new Date();
const second=(endTime.getTime()-startTime.getTime())/1000;
duration+=second;
flag=0;
}
else{
console.log('You have not started the watch');
}
}
this.reset=function(){
endTime=null
startTime=null
flag=0;
duration=0;
}
Object.defineProperty(this,'duration',{
get:function(){
return duration;
}
});
};
}