how to make a timer in javascript stack overflow code example

Example 1: countdown timer javascript stack overflow

var hms = "02:30:00";
	var a = hms.split(':'); // split it at the colons

	// minutes are worth 60 seconds. Hours are worth 60 minutes.
	var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); 

		if(seconds > 0)
		{	

	      function secondPassed() {
	      	  
	      	  
	          var minutes = Math.round((seconds - 30)/60),
	              remainingSeconds = seconds % 60;
                   var hour   =Math.floor((minutes)/60);
                    minutes = minutes%60;

	          if (remainingSeconds < 10) {
	              remainingSeconds = "0" + remainingSeconds;
	          }
              hour = ("0" + hour).slice(-2);
              minutes = ("0" + minutes).slice(-2);
              remainingSeconds= ("0" + remainingSeconds).slice(-2);

	          document.getElementById('countdown').innerHTML = hour +":" +minutes + ":" + remainingSeconds;
	          if (seconds == 0) {
	              clearInterval(countdownTimer);
	             //form1 is your form name
	            document.form_quiz.submit();
	          } else {
	              seconds--;
	          }
	      }
	      var countdownTimer = setInterval('secondPassed()', 1000);

	      }

Example 2: countdown timer javascript stack overflow

<html>
<body>
<div id="hms">02:00:00</div>
</body>

<script>
var startTime;
function getCookie(name) {
  var value = "; " + document.cookie;
  var parts = value.split("; " + name + "=");
  if (parts.length == 2) return parts.pop().split(";").shift();
} // credits kirlich @http://stackoverflow.com/questions/10730362/get-cookie-by-name

function count() {
 if(typeof getCookie('remaining')!= 'undefined')
 {
   startTime = getCookie('remaining');
 }
 else if(document.getElementById('hms').innerHTML.trim()!='')
 {
   startTime = document.getElementById('hms').innerHTML;
 }
 else
 {
  var d = new Date(); 
  var h=d.getHours(); 
  var m=d.getMinutes();
  var s=d.getSeconds();
  startTime = h+':'+m+':'+s;
  //OR
  startTime  = d.toTimeString().split(" ")[0]
 }

 var pieces = startTime.split(":");
 var time = new Date();
 time.setHours(pieces[0]);
 time.setMinutes(pieces[1]);
 time.setSeconds(pieces[2]);
 var timediff = new Date(time.valueOf()-1000)
 var newtime = timediff.toTimeString().split(" ")[0];
 document.getElementById('hms').innerHTML=newtime ;
 document.cookie = "remaining="+newtime;
 setTimeout(count,1000);
}
count();
</script>

</html>