Countdown (JavaScript)

This script counts down from a specified number of minutes.

<html>
<head>
<title>Countdown</title>
<script type="text/javascript">
// how many minutes
var mins = 30;

// how many seconds (don't change this)
var secs = mins * 60;
function countdown() {
	setTimeout('Decrement()',1000);
}
function Decrement() {
	if (document.getElementById) {
		minutes = document.getElementById("minutes");
		seconds = document.getElementById("seconds");
		// if less than a minute remaining
		if (seconds < 59) {
			seconds.value = secs;
		} else {
			minutes.value = getminutes();
			seconds.value = getseconds();
		}
		secs--;
		setTimeout('Decrement()',1000);
	}
}
function getminutes() {
	// minutes is seconds divided by 60, rounded down
	mins = Math.floor(secs / 60);
	return mins;
}
function getseconds() {
	// take mins remaining (as seconds) away from total seconds remaining
	return secs-Math.round(mins *60);
}
</script>
</head>
<body onload="countdown();">
	Countdown: <input id="minutes" type="text" style="width: 22px;"> minutes <input id="seconds" type="text" style="width: 22px"> seconds remaining.
</body>
</html>

Comments

Anonymous said…
Thanks a lot for this neat, simple countdown script!
Ovidiu U said…
thank you, just what I was looking for.
Mohammad Hassan said…
how i can stop at 00:00

right now it is going -1:59
Sam said…
Not tested, but try changing these lines:

secs--;
setTimeout('Decrement()',1000);


To

secs--;
if(secs > 0)
{
setTimeout('Decrement()',1000);
}

Sorry for late response.

Popular posts from this blog

Select box manipulation with jQuery

iTextSharp: Generating a Basic PDF file (ASP.NET/C#)

Popup Window Countdown (JavaScript)