Changing Javascript Counter From Seconds To Milliseconds -


so fiddling around code found week ago due ability 'somewhat' accomplish of old gmail file counter did. i've failed make counter accurately increase @ faster speed other per seconds.

by editing lowest 1000 [found @ settimeout(function(){thisobj.start()}, 1000) ] in code counter counts faster when refreshed reverts near started additional second or 2 (depending on time start refresh).

<html> <head>     <script type="text/javascript">         function countup(startingdate, base) {             this.currenttime = new date();             this.startingdate = new date(startingdate);             this.base = base;             this.start();         }          countup.prototype.oncountup = function(){};          countup.prototype.start = function() {             var thisobj = this,                             timediff = (this.currenttime-this.startingdate) / 1000,                 secondfield = math.floor((timediff));                 result = { seconds: secondfield };              this.currenttime.setseconds(this.currenttime.getseconds() + 1);             this.oncountup(result);              settimeout(function(){                 thisobj.start();             }, 1000);         };     </script> </head>  <body>     <div id="holder"></div>      <script type="text/javascript">         var startdate = new countup("august 3, 2013 18:59:00", "seconds") // change starting date here          startdate.oncountup= function(result) {             var mycountainer = document.getelementbyid("holder");             mycountainer.innerhtml =+ result['seconds'];         }     </script> </body> </html> 

i'm can changed count milliseconds increasing @ faster speed amount per second.

thanks in advance!

in line:

this.currenttime.setseconds(this.currenttime.getseconds() + 1); 

the code update currenttime, setting 1 second forward. function executed every second, effectly works it's getting current time.

if change run ever .5 seconds, advance seconds twice fast should, causing regress when refresh page. if let run 3 seconds, add 6 seconds instead, if reload clock seem go 3 seconds when getting current time.

for reason, code doesn't current date calling new date() instead (performance? memory management?), you'll have change how moves currenttime forward fix bug, making increments in milliseconds instead of seconds, this:

    this.currenttime.setmilliseconds(this.currenttime.getmilliseconds()+500);     var timediff=(this.currenttime-this.startingdate)/500;     var secondfield=math.floor((timediff));     var result={halfseconds:secondfield};     this.oncountup(result);     settimeout(function(){thisobj.start()}, 500); 

that make increment 1 every .5 seconds.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -