-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountDown.js
53 lines (44 loc) · 1.29 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
var secondsRemaining;
var intervalHandle;
function resetPage(){
document.getElementById("inputArea").style.display = "block";
}
function tick(){
var timeDisplay = document.getElementById("time");
var min = Math.floor(secondsRemaining/60);
var sec = secondsRemaining - min*60;
if (sec<10){
sec = "0" + sec;
}
var message = min + ":"+ sec;
timeDisplay.innerHTML = message;
if (secondsRemaining === 0){
alert("wake up");
clearInterval(intervalHandle);
resetPage();
}
secondsRemaining--;
}
function startCountdown(){
var minutes = document.getElementById("minutes").value;
if (isNaN(minutes)) {
alert("please enter a number");
return;
}
secondsRemaining = minutes*60;
intervalHandle = setInterval(tick, 1000);
document.getElementById("inputArea").style.display="none";
}
window.onload = function(){
var inputMinutes = document.createElement("input");
inputMinutes.setAttribute("id", "minutes");
inputMinutes.setAttribute("type", "text");
var startButton = document.createElement("input");
startButton.setAttribute("type", "button");
startButton.setAttribute("value", "start countdown");
startButton.onclick = function(){
startCountdown();
};
document.getElementById("inputArea").appendChild(inputMinutes);
document.getElementById("inputArea").appendChild(startButton);
};