javascript - A counter made in Node.js which changes its value at definite time intervals -


i asking problem not able solve despite every effort. trying make counter. counter placed in div under available items on webpage. counter should able change value @ predefined values , intervals, example, value starts @ 5,000 @ decreases 1 after 2 seconds , 4 after next 5 seconds , 3 after next 2 seconds , process repeats (decreases 1 after 2 seconds...) 3 or 4 sets of variation enough. value shown counter must not affected number of page loads or simultaneous users, should remain if user sees page in 2 different browsers, must shown described above. counter must capable of changing value without needing user refresh page.

most straightforward solution appears me make timer relative absolute time.

this means take time passed since arbitrary point in time define start, e.g. right var start = date("thu jun 04 2015 01:46:44 gmt+0200 (cest)") here. can subtract start current time learn how time has passed:

var passedseconds = (new date() - start) / 1000; 

we divide 1000 since js calculates miliseconds default.

to update timer, simple

setinterval(function() {      var passedseconds = (new date() - start) / 1000;      var value = docalculationforseconds(passedseconds);      document.getelementbyid('mydisplay)'.textcontent = value; }, 1000); 

which calls method every second.

lastly, need figure out way calculate progress. either run formula in big loop of passedseconds. or evaluate if can reduce single calculation step. depend on exact changes in value you'll have in final version.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Website Login Issue developed in magento -

Can the constants be defined inside a model file of a framework in PHP? -