forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts-START.js
More file actions
62 lines (55 loc) · 1.65 KB
/
scripts-START.js
File metadata and controls
62 lines (55 loc) · 1.65 KB
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
var Timer = (function() {
var mainTimer = document.getElementsByClassName("display__time-left")[0];
var timeForm = document.customForm.minutes;
var countDown;
var startTime;
var endTime;
var timer = function() {
var secondsLeft = Math.round((endTime - Date.now()) / 1000);
if (secondsLeft < 0) {
clearInterval(countDown);
return;
}
displayTimeLeft(secondsLeft);
};
var displayTimeLeft = function(seconds) {
var hr = Math.floor(seconds / 3600);
var min = Math.floor(seconds / 60);
var sec = seconds % 60;
if (hr > 0) {
min = min > 9 ? min.toString() : '0' + min;
}
sec = sec > 9 ? sec.toString() : '0' + sec;
var timeString = hr > 0 ? hr + ':' + min + ':' + sec : min + ':' + sec;
mainTimer.textContent = timeString;
document.title = timeString;
};
return {
startTimer: function(seconds) {
clearInterval(countDown);
startTime = Date.now();
endTime = startTime + seconds * 1000;
displayTimeLeft(seconds);
countDown = setInterval(timer, 1000);
},
attachHandlers: function() {
// handle pre-set buttons
var buttons = document.getElementsByClassName('timer__button');
for (var i=0; i < buttons.length; i += 1){
buttons[i].addEventListener('click', function() {
Timer.startTimer(this.dataset['time']);
});
}
// handle user-entry box
document.customForm.addEventListener('submit', function(e){
e.preventDefault();
var runTime = Math.round(this.minutes.value * 60);
if (!isNaN(runTime)) {
Timer.startTimer(runTime);
}
this.reset();
});
}
};
}());
Timer.attachHandlers();