views:

76

answers:

3

Hi, guys! I'm developing an online auction with time limit.

The ending time period is only for one opened auction.

After logging into the site I show the time left for the open auction. The time is calculated in this way:

EndDateTime = Date and Time of end of auction;
DateTime.Now() = current Date and Time

timeLeft= (EndDateTime - DateTime.Now()).Seconds().

In javascript, I update the time left by:

timeLeft=timeLeft-1

The problem is that when I login from different browsers at the same time the browsers show a different count down.

Help me, please!

+1  A: 

I guess there will always be differences of a few seconds because of the server processing time and the time needed to download the page.

The best way would be to actually send the end time to the browser and calculate the time remaining in javascript. That way the times should be the same (on the same machine of course).

CyberDude
+1, but I would send both the ending time AND the current date time to the browser. That way you don't depend on the client machine to be in sync with the rest of the world.. which they frequently aren't
Chris Lively
Also, I would have an ajax request which fetched an update from the server every 10 seconds or so. This way if the client browser's timer resolution is out of whack, then it gets back on track fairly quickly.
Chris Lively
indeed one needs to take into account the timezone differences, very important!
CyberDude
i would simplify that by sending down 'just' the seconds remaining from the server (or in fact, 'milliseconds' remaining). then everyone is on the same base, irrespective of timezones.
jim
that's exactly what he's doing now but there is a slight delay from when the remaining seconds are calculated on the server and until the javascript starts counting downwards in the browser
CyberDude
sorry, i thought that he was using the DateTime object inside the <%%> tags inside his javascript on the page, hence the hoo ha about different timezones etc..
jim
`timeLeft=timeLeft-1` will inevitably lead to problems. I would always compare the current time against the end time instead, and wrap the comparison in a `setTimeout(1000)` or so.
bzlm
A: 

This may be a problem with javascript loading at different speeds, or the setInterval will trigger at slightly different times depending on the loop

i would look into those two

Hurricanepkt
A: 

Roman,

I had a little look at eBay (they know a thing or two about this stuff :)) and noticed that once the item is inside the last 90 seconds, a GET request gets fired every 2 seconds to update the variables in the javascript via a json response. you can look at this inside firebug/fiddler to see what it does.

here is an example of the json it pulls down:

{
   "ViewItemLiteResponse":{
      "Item":[
         {
            "IsRefreshPage":false,
            "ViewerItemRelation":"NONE",
            "EndDate":{
               "Time":"12:38:48 BST",
               "Date":"01 Oct, 2010"
            },
            "LastModifiedDate":1285932821000,
            "CurrentPrice":{
               "CleanAmount":"23.00",
               "Amount":23,
               "MoneyStandard":"&#163;23.00",
               "CurrencyCode":"GBP"
            },
            "IsEnded":false,
            "AccessedDate":1285933031000,
            "BidCount":4,
            "MinimumToBid":{
               "CleanAmount":"24.00",
               "Amount":24,
               "MoneyStandard":"&#163;24.00",
               "CurrencyCode":"GBP"
            },
            "TimeLeft":{
               "SecondsLeft":37,
               "MinutesLeft":1,
               "HoursLeft":0,
               "DaysLeft":0
            },
            "Id":160485015499,
            "IsFinalized":false,
            "ViewerItemRelationId":0,
            "IsAutoRefreshEnabled":true
         }
      ]
   }
}

You could do something similar inside your code.

[edit] - on further looking at the eBay code, altho it only runs the intensive GET requests in the last 90 seconds, the same json as above is added when the page is initially loaded as well. Then, at 3 mins or so, the GET request is run every 10 seconds. therefore i assume the same javascript is run against that structure whether it be >90 seconds or not.

jim
i presume this info was useless then :)
jim