-
Notifications
You must be signed in to change notification settings - Fork 1
/
StopWatch.js
60 lines (51 loc) · 1.68 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Prevent multiple clicks on start
var isSet=false;
// Time at start (Returns present time wrt 1970)
var timeatStart=timeatStop=0;
function timeNow(){
return (new Date()).getTime();
}
// Declaring variables
var seconds=minutes=hours=milliseconds=newTime=0;
// Function to execute when start button is clicked
function startClick(){
if(!isSet){
isSet=true;
timeatStart=(new Date()).getTime();
$int=setInterval(function(){timerUpdate(timeatStart)},1);
}
}
// Function to execute when stop button is clicked
function stopClick(){
isSet=false;
clearInterval($int);
timeatStop+=(new Date()).getTime()-timeatStart;
}
// Function to execute when reset button is clicked
function resetClick(){
clearInterval($int);
isSet=false;
timeatStart=timeatStop=0;
seconds=minutes=hours=milliseconds=0;
displayTime(hours,minutes,seconds,milliseconds);
}
// Calculate time
function timerUpdate(timeatStart){
newTime = (new Date()).getTime();
timeEllapsed = newTime+timeatStop-timeatStart;
hours = Math.floor(timeEllapsed/(3600000));
timeEllapsed=timeEllapsed%(3600000);
minutes = Math.floor(timeEllapsed/(60000));
timeEllapsed=timeEllapsed%(60000);
seconds = Math.floor((timeEllapsed/1000));
milliseconds = timeEllapsed%1000;
displayTime(hours,minutes,seconds,milliseconds);
}
// Zero padding
function padZero(number, digits) {
return Array(Math.max(digits - String(number).length + 1, 0)).join(0) + number;
}
// Display time on the timerboard
function displayTime(hours,miutes,seconds,milliseconds){
document.getElementById("timerBoard").innerHTML = padZero(hours,2)+":"+padZero(minutes,2)+":"+padZero(seconds,2)+":"+padZero(milliseconds,3);
}