0

Possible Duplicate:
How to sync a javascript countdown with server time

I'm using C# .Net MVC 4 for an application

I have a server side function that calculates the amount of "Time Left" in seconds. This amount gets passed to the View for two purposes. 1. to display it and 2. To start a javascript setInterval routine that will count down the time left client side.

I've noticed there is a time delay between the calculation and the actual start of the setInterval countdown.

What is the best way to capture the amount of time between the calculation and the start of the setInterval so that the time left can be offset by that delay?

Community
  • 1
  • 1
SLoret
  • 1,531
  • 3
  • 18
  • 31
  • While it does look like a duplicate, none of those answers solve my problem. Even the ajax answer is assuming all is well and there is only a small time delay. – SLoret Oct 02 '12 at 22:35
  • Latency should be in < 80 milliseconds... Does this really need to be that exact? Are you displaying milliseconds to the user? – dqhendricks Oct 02 '12 at 23:07

1 Answers1

0

To calculate the amount of time it takes for arbitrary code to run:

var start = new Date();

... // arbitrary code;

var timeElapsed = new Date() - start; // time in ms
Shmiddty
  • 13,847
  • 1
  • 35
  • 52
  • This sounds like a latency issue between the web server and the browser, or waiting for the browser to render the DOM and start the javascript in the first place. This won't work, because he won't know the start time from javascript. – Joel Coehoorn Oct 02 '12 at 22:47
  • If there isn't a postback, there's nothing stopping him from applying this to an asynchronous call... – Shmiddty Oct 02 '12 at 22:48