-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
71 lines (60 loc) · 1.32 KB
/
main.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
var start = document.querySelector('.start');
var pause = document.querySelector('.pause');
var reset = document.querySelector('.reset');
var minRef = document.querySelector('.min');
var secRef = document.querySelector('.sec');
var state = {
min: '00',
sec: '00'
};
var id;
setTime(state);
function computeNextTime(state) {
var min = state.min;
var sec = state.sec;
if (sec === 59) {
sec = '00';
min++;
}
else {
sec++;
}
return {
min: min,
sec: sec
};
}
function setTime(state) {
var min = state.min;
var sec = state.sec;
minRef.textContent = min;
secRef.textContent = sec;
}
function handleStart() {
if(id==undefined)
id=setInterval(function () {
var newState = computeNextTime(state);
state = newState;
setTime(state);
}, 1000);
}
function handlePause() {
if(id===undefined)
{
return;
}
clearInterval(id);
id=undefined;
}
function handleReset() {
clearInterval(id);
state={
min: '00',
sec: '00'
};
setTime(state);
id=undefined;
}
start.addEventListener('click', handleStart);
pause.addEventListener('click', handlePause);
reset.addEventListener('click', handleReset);