CountDown

Sample and test

by DevWithCoffee

HTML

<input id="timer" type="text" value="" /><button type="button" onclick="startCounter()">Restart</button>

JavaScript

var countdown, counter;

function showTimer(){
	var timer = document.getElementById("timer");
	if(countdown > 59){
		timer.value = parseInt(countdown/60) + " Min (" + countdown + " Secs)";
	}else{
		timer.value = countdown + " Secs"; /* Mostra apenas segundos */
	}
	if(countdown == 0){
		clearInterval(counter); /* Para o timer */
	}
}

function startCounter(){
	countdown = 121;
  clearInterval(counter); /* zera antes de iniciar um novo */
	showTimer(); /* Mostra o timer logo que carregar */
	counter=setInterval(function(){
		countdown--;
		showTimer();
	}, 300);
}

document.addEventListener("load", startCounter());