<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
var interval;
var minutes = 5;
var seconds = 10;
window.onload = function() {
countdown('countdown');
}
function countdown(element) {
interval = setInterval(function() {
var el = document.getElementById(element);
if(seconds == 0) {
if(minutes == 0) {
alert(el.innerHTML = "countdown's over!");
clearInterval(interval);
return;
} else {
minutes--;
seconds = 60;
}
}
if(minutes > 0) {
var minute_text = minutes + (minutes > 1 ? ' minutes' : ' minute');
} else {
var minute_text = '';
}
var second_text = seconds > 1 ? 'seconds' : 'second';
el.innerHTML = minute_text + ' ' + seconds + ' ' + second_text + ' remaining';
seconds--;
}, 1000);
}
</script>
</head>
<body>
<div id='countdown'></div>
</body>
</html>
<input type="button" onclick="countdown('countdown');" value="Start" />
<input type="button" onclick="minutes=5;seconds=10;" value="Reset" />
window.onload = function() {
countdown('countdown');
}
<a href="javascript:void(0);" onclick="countdown('countdown')">Click Me to Start</a>
var interval;
<a href="javascript:void(0);" onclick="clearInterval(interval)">Click Me to Stop</a>
<input type="button" value="reset" id="reset" />
<input type="button" value="start" id="start" />
var reset = document.getElementById('reset');
reset.onclick = function() {
minutes = 5;
seconds = 10;
clearInterval(interval);
interval = null;
}
var start = document.getElementById('start');
start.onclick = function() {
if (!interval) {
countdown('countdown');
}
}