I need a simple script for JavaScript countdown for quiz preparation.
- Initially quiz page loads, countdown timer to be start for 30mins
- After 30mins over, it shows alert message to user.
Below is the code which am using, but after 30mins it decrementing..
<html>
<head>
<title>Countdown</title>
<script type="text/javascript">
var mins = 30;
var secs = mins * 60;
function countdown() {
setTimeout('Decrement()',1000);
}
function Decrement() {
if (document.getElementById) {
minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
if (seconds < 59) {
seconds.value = secs;
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
secs--;
setTimeout('Decrement()',1000);
}
}
function getminutes() {
mins = Math.floor(secs / 60);
return mins;
}
function getseconds() {
return secs-Math.round(mins *60);
}
</script>
</head>
<body>
<div id="timer">
This is only valid for the next <input id="minutes" type="text" style="width: 14px; border: none; background-color:none; font-size: 16px; font-weight: bold;"> minutes and <input id="seconds" type="text" style="width: 26px; border: none; background-color:none; font-size: 16px; font-weight: bold;"> seconds.
</div>
<script>
countdown();
</script>
Thanks in Advance.