Time counter – using JavaScript

      No Comments on Time counter – using JavaScript

Time counter

See the Pen Time counter in JavaScript by scanfcode (@scanfcode) on CodePen.


Here it is:

<div class="col-md-4">
<div class="clock_in"><span id="d">Days</span><br /><span>Days</span></div>
<div class="clock_in"><span id="h">Hours</span><br /><span>Hours</span></div>
<div class="clock_in"><span id="m">Minutes</span><br /><span>Minutes</span></div>
<div class="clock_in"><span id="s">Sec</span><br /><span>Seconds</span></div>
</div>
<button onclick="starttime();">Start Clock</button>
<script>
 function starttime()
 {
  var endtime = '2017-03-30';
  var t = Date.parse(endtime) - Date.parse(new Date());
  var seconds = Math.floor( (t/1000) % 60 );
  var minutes = Math.floor( (t/1000/60) % 60 );
  var hours = Math.floor( (t/(1000*60*60)) % 24 );
  var days = Math.floor( t/(1000*60*60*24) );
  

  document.getElementById('d').innerHTML=days;  
  document.getElementById('h').innerHTML=hours;  
  document.getElementById('m').innerHTML=minutes;  
  document.getElementById('s').innerHTML=seconds;  
  var q = setTimeout(starttime, 500);
}
</script>

Auto start time counter

<body onload="starttime();">
/* code here */
</body>

or

<script>
$(window).on("load",function(){
starttime();
});
</script>

Leave a Reply

Your email address will not be published. Required fields are marked *