1

I need a Timer for a website I'm making. There are Timer's that I've used before, (only, in Windows Forms and WPF apps) - not in Web Development. So I'm not quite sure about which Timer would be the most appropriate, and most accurate one to use in a Web page.

I have a few requirements, which are:

  1. It must be as accurate as possible
  2. It needs to be a Timer based on a Time period, I.e: 2:00:00

Kind of like how those timers work on bidding sites.

I'm not looking for any samples (although that would be appreciated if you've got some laying around somewhere) - I'm just not sure where to look, or what one would be the best one for the job.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Jason
  • 139
  • 3
  • 10
  • 1
    The timer class you use doesn't really have anything to do with javascript or AJAX if you're looking for a C# class. – Merlyn Morgan-Graham Jan 01 '12 at 04:57
  • Is there anything wrong with `DateTime.UtcNow`? It sounds like it is just for a website. Client's latency to the server means you probably won't need to keep time much more accurate than some large-ish fraction of a second. – Merlyn Morgan-Graham Jan 01 '12 at 05:23
  • http://stackoverflow.com/questions/1485839/high-performance-timer-vs-stopwatch – kenny Jan 01 '12 at 13:50

2 Answers2

2

If by web development you mean JavaScript you can get the current time in milliseconds using the Date object.

The level of accuracy depends on the browser and/or device.

Here is an example of using it for profiling:

var startTime = new Date().getTime();
// some long running operation
var endTime = new Date().getTime();
var ellapsedMilliseconds = endTime - startTime;
Luis Perez
  • 27,650
  • 10
  • 79
  • 80
1

Kind of like how those timers work on bidding sites.

A timer like that would use roughly accurate JavaScript client side script to generate a clock, but the results would need to be validated server-side to be trustworthy (for example, you don't want me sending you an HTTP request saying that I really was the last bidder on an eBay item without checking it against something I can't spoof).

A simple timer could just rely on setTimeout() or setInterval(). They aren't truly exact by any means but they will fire at approximate times that are close enough for most uses.

To perform an reasonably accurate client-side countdown that updates a client-side clock:

  • Set a starting date variable
  • Check the current date every 100ms using setInterval()
  • Compare the different and see if some time period has elapsed
  • Perform some action when the time period has elapsed (and make sure to call clearInterval())

Again, you must validate your timer results server-side if you want them to be tamper-resistant. You might asynchronously communicate with the server very regularly, or just when a countdown is reached.

Tim M.
  • 53,671
  • 14
  • 120
  • 163
  • Thanks for your detailed answer @Tim M, you've certainly given me a bit to think about and more than enough to get me started. Thanks again! – Jason Jan 01 '12 at 06:10