-
Notifications
You must be signed in to change notification settings - Fork 0
/
countdown.js
87 lines (62 loc) · 1.48 KB
/
countdown.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
82
83
84
85
86
// Javascript Countdown Timer
var countFrom;
var inSeconds;
var counter;
var submit = document.getElementById("submit");
var theTime = document.getElementById("theTime");
var time = document.getElementById("time");
function resetPage(){
submit.style.display = "inline-block";
submit.innerHTML = "DO IT AGAIN!";
theTime.style.display = "inline-block";
theTime.value = "";
time.innerHTML = "0:00";
}
function changeColour(){
if ((inSeconds < 30) && (inSeconds > 20)){
time.style.color = "#FF8000";
}
if ((inSeconds < 20) && (inSeconds > 10)){
time.style.color = "#FF4000";
}
if ((inSeconds < 10) && (inSeconds > 0)){
time.style.color = "#DF0101";
}
}
function convertToTime(){
var min = Math.floor(inSeconds / 60);
var secs = inSeconds - (min * 60);
if (secs < 10){
secs = "0" + secs;
}
changeColour();
counter = min + ":" + secs;
}
function theCount(){
convertToTime();
if (inSeconds >= 0){
inSeconds--;
}
if (inSeconds === -1){
resetPage();
clearInterval(intervalHandle);
}
time.innerHTML = counter;
}
document.getElementById("submit").onclick = function(){
time.style.color = "#000000";
countFrom = theTime.value;
if (isNaN(countFrom)){
alert("You didn't enter a number!");
return;
}
inSeconds = countFrom * 60;
convertToTime();
time.innerHTML = counter;
inSeconds--;
intervalHandle = setInterval(theCount, 1000);
submit.style.display = "none";
theTime.style.display = "none";
time.style.display = "block";
return false;
}