Count Down Timer with Mins : Secs

by Faisal Khan Janjua

HTML

<h1></h1>
<button id="start">Start Timer</button>
<button id="clear">Clear Timer</button>

JavaScript

var totalSeconds = 215;

function formatTime(parm){
	var minutes = Math.floor(parm / 60);
  var seconds = parm - minutes * 60;
  if(minutes<10){
  	minutes = '0'+minutes;
  }
  if(seconds<10){
  	seconds = '0'+seconds;
  }
  return minutes +':'+ seconds;
}
jQuery('h1').text(formatTime(totalSeconds));

var timer, secondsNow;
secondsNow = totalSeconds;
timer = setInterval(function(){
  secondsNow--;
  if (secondsNow < 1) { clearInterval(timer); }
  jQuery('h1').text(formatTime(secondsNow));
}, 1000);

$(document).on('click', '#start', function(){
	secondsNow = totalSeconds;
  jQuery('h1').text(formatTime(secondsNow));
  timer = setInterval(function(){
    secondsNow--;
    if (secondsNow < 1) { clearInterval(timer); }
    jQuery('h1').text(formatTime(secondsNow));
  }, 1000);
});

$(document).on('click', '#clear', function(){
	clearInterval(timer);
});