Find elapsed time in javascript -


i'm new javascript , i'm trying write code calculates time elapsed time user logged in current time.

here code:-

function markpresent() {     window.markdate = new date();     $(document).ready(function() {         $("div.absent").toggleclass("present");     });     updateclock(); }  function updateclock() {         var markminutes = markdate.getminutes();     var markseconds = markdate.getseconds();      var currdate = new date();     var currminutes = currdate.getminutes();     var currseconds = currdate.getseconds();     var minutes = currminutes - markminutes;     if(minutes < 0) { minutes += 60; }     var seconds = currseconds - markseconds;     if(seconds < 0) { seconds += 60; }      if(minutes < 10) { minutes = "0" + minutes; }     if(seconds < 10) { seconds = "0" + seconds; }      var hours = 0;     if(minutes == 59 && seconds == 59) { hours++; }     if(hours < 10) { hours = "0" + hours; }      var timeelapsed = hours+':'+minutes+':'+seconds;     document.getelementbyid("timer").innerhtml = timeelapsed;     settimeout(function() {updateclock()}, 1000); } 

the output correct upto 00:59:59 after that o/p is:

00:59:59 01:59:59 01:59:00 01:59:01 . . . . 01:59:59 01:00:00

how can solve , there more efficient way can this? thank you.

there's going on here.

an easier way compare markdate current date each time , reformat.

see demo: http://jsfiddle.net/7e4psrzu/

function markpresent() {     window.markdate = new date();     $(document).ready(function() {         $("div.absent").toggleclass("present");     });     updateclock(); }  function updateclock() {       var currdate = new date();     var diff = currdate - markdate;     document.getelementbyid("timer").innerhtml = format(diff/1000);     settimeout(function() {updateclock()}, 1000); }  function format(seconds) { var numhours = parseint(math.floor(((seconds % 31536000) % 86400) / 3600),10); var numminutes = parseint(math.floor((((seconds % 31536000) % 86400) % 3600) / 60),10); var numseconds = parseint((((seconds % 31536000) % 86400) % 3600) % 60,10);     return ((numhours<10) ? "0" + numhours : numhours)     + ":" + ((numminutes<10) ? "0" + numminutes : numminutes)     + ":" + ((numseconds<10) ? "0" + numseconds : numseconds); }  markpresent(); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -