Timer JavaScript Example Code

Best of timer in JavaScript – preciseness in milisecond

// global variables
var _startDate = null;
var _stopDate = null;
var go = null; // timing
 
function startFC(){
_startDate = new Date();
if(go==null)timingFC();
}
 
timingFC(){
if(go==null){
   _startDate = new Date();
//   document.getElementById('idStartTime').innerHTML= _startDate.toLocaleTimeString();
   go=setInterval("s()",1000); // every 1 second == 1000 milisecond call function s()
 
   }
}
 
function s(){
var currentMiliSecond =  new Date() - _startDate;
var currentSecond = currentMiliSecond / 1000;
document.getElementById('idCurrentTiming').innerHTML= currentSecond.toString();
}
 
function stopFC(){
clearTimeout(go); // stop calling of s()
_stopDate = new Date();
var timeInMiliSecond = _stopDate - _startDate;
var timeInSecond = timeInMiliSecond / 1000;
var timeInMinute = timeInSecond / 60;
 
}
This entry was posted in Javascript css html and tagged , . Bookmark the permalink.


Comments are closed.



4,011 views