0

I'm trying to create a continuously increasing number on my site that starts at a given number (5,000,000 for instance) and increases at an even rate (of about 1 per sec or so) to infinity. This should happen independent of the user loading the webpage -- in other words, the timer should NOT restart at 5,000,000 every time you load the webpage. It would instead show the new higher number.

I was thinking the easiest way (in my very limited knowledge on the subject) might be to use a previous date as a starting point and count the seconds elapsed from that date. That way it would be accurate no matter when you load the page and would increase at 1 per second.

Any ideas?

eis
  • 51,991
  • 13
  • 150
  • 199
Patrick Mauldin
  • 139
  • 1
  • 1
  • 8
  • Store the time in a cookie on first load and use this to calculate your number. – Lee Taylor Dec 14 '13 at 01:59
  • Wouldn't this give different people different numbers as the cookie is only stored on each individual computer? – Patrick Mauldin Dec 14 '13 at 02:07
  • you're right. your best bet is to make it a function of the actual time/date. – ltiong_sh Dec 14 '13 at 02:10
  • I think the only reliable way to do this is using a date and calculating the difference between that date and now, in seconds, and then adding those seconds to the pre-set number – Jason Dec 14 '13 at 02:10
  • You are going to have to do it based on a time/date and have that value based on the server's date time, which might look "off" to a end user depending on their time zone. – Mark Dec 14 '13 at 02:11
  • @Jason I think thats the best route. Do you know of a good plugin or something to accomplish this? All of the ones I found didn't quite do what I need. – Patrick Mauldin Dec 14 '13 at 02:13

1 Answers1

0
var initSeconds = new Date(2010, 0).getSeconds() //january 1 of 2010
    , BASE_NUMBER = 5000000
;

setInterval(function() {
   var curSeconds = new Date().getSeconds()
       , elem = $('#mydisplayelem')
   ;
   elem.text(BASE_NUMBER + curSeconds - initSeconds);
}, 1000);

Something along these lines... this won't do exactly what you want, you need a better time calculation, but this should get you started.

Also, see this article about calculating date diffs.

Jason
  • 51,583
  • 38
  • 133
  • 185