CountDown

Sample and test

by DevWithCoffee

HTML

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

JavaScript

var countdown = 121; /*Tempo inicial*/
var 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(){
	showTimer(); /* Mostra o timer logo que carregar */
	counter=setInterval(function(){
		countdown--;
		showTimer();
	}, 300);
}