-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexercise9.js
49 lines (37 loc) · 1.32 KB
/
exercise9.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
/**
* Exercise #9: Countdown timer
*/
var timeLeft = -1; // global variable, stores time left (in seconds)
var myTimer; // holds the ID returned by setInterval
// converts remaining time (seconds) to MM:SS format with leading zeros
function timeLeftToString() {
var m = Math.floor(timeLeft / 60);
var s = timeLeft - 60 * m;
// add leading zeros
return ((m < 10) ? "0" : "") + m + ":" + ((s < 10) ? "0" : "") + s;
}
// updates the counter and the progress bar
function updateTimer() {
timeLeft--;
$("#timer").html(timeLeftToString());
$("#progressbar").attr("value", timeLeft);
if (timeLeft == 0) {
alert("Time's up!");
clearInterval(myTimer); // stop timer
}
}
$(document).ready(function () {
$("form[name='countdown_form']").submit(function(e) {
e.preventDefault(); // preventing the form from submission
// set time left (seconds)
timeLeft = $("#minutes").val() * 60;
$(this).hide(); // hide form
// initialize timer and progress bar, then show them
$("#timer").html(timeLeftToString());
$("#progressbar").attr("max", timeLeft);
$("#progressbar").attr("value", timeLeft);
$("#countdown").show();
// update the timer in every second
myTimer = setInterval(updateTimer, 1000);
});
});