-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.js
81 lines (74 loc) · 2.04 KB
/
timer.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var timer;
var state = 0;
var stoptime = 0;
var lapcounter = 0;
var current;
var lapdate = ""
var lapdetails;
function timecounter(starttime){
currentdate = new Date();
lapdetails = document.getElementById('lapdetails');
timer = document.getElementById('timer');
var timediff = currentdate.getTime() - starttime;
if(state == 0) {
timediff = timediff + stoptime;
}
if(state == 1) {
var time = document.getElementById('time');
if(timediff >= time.value*60*1000) { //10 min = 600000
lapdetails.value += 'time up';
startstop();
}
timer.value = formattedtime(timediff);
refresh = setTimeout(timecounter, 10, starttime)
}
else {
window.clearTimeout(refresh);
stoptime = timediff;
}
}
function marklap() {
if(state == 1) {
lapdate = timer.value;
lapdetails.value += (++lapcounter) + '. ' + timer.value + '\n';
lapdetails.scrollTop = lapdetails.scrollHeight;
}
}
function startstop() {
var startstop = document.getElementById('startstopbutton');
var startdate = new Date();
var starttime = startdate.getTime();
if(state == 0) {
startstop.value = 'Stop';
state = 1;
timecounter(starttime);
}
else {
startstop.value = 'Start';
state = 0;
lapdate = '';
}
}
function reset() {
lapdetails.value = '';
lapcounter = 0;
stoptime = 0;
lapdate = '';
window.clearTimeout(refresh);
if(state == 1){
var resetdate = new Date();
var resettime = resetdate.getTime();
timecounter(resettime);
}
else {
timer.value = "0:0:0";
}
}
function formattedtime(unformatted) {
var decisecond = Math.floor(unformatted/100) + ''; // accurate to tenth of second
var second = Math.floor(unformatted/1000) ;
var minute = Math.floor(unformatted/60000);
decisecond = decisecond.charAt(decisecond.length - 1);
second = second - 60*minute + '';
return minute + ':' + second + ':' + decisecond;
}