admin管理员组

文章数量:1418094

Is there some window variable in javascript that I can use or a way to use a timer to track elapsed time without using a date object? flash.utils.getTimer works great in the flash version of this application and I'm trying to port it to javascript. I can't risk changes to the system clock causing security problems, so I don't want to use the Date object.

I have a servertimestamp and I would like to create a timer that is independent of the operating system time to calculate the current server time. I'll use this calculated value to allow different operations during windows on the server time. I'm not concerned about javascript hacking as I know there's no security there, but I would like to prevent the casual user from messing around with the availability of certain features.

Thanks

Added for clarification:

This project relates to live video events. The live video has a start time relative to the server and through another webservice I am able to grab the server time. What I need to do is to maintain this server time on the client machine independently of the user's local time to determine when the live stream should start and stop on the client. So the problem isn't really the format that I store the server time, it's how do I get the elapsed time from when I grabbed the server time without using a date object.

Is there some window variable in javascript that I can use or a way to use a timer to track elapsed time without using a date object? flash.utils.getTimer works great in the flash version of this application and I'm trying to port it to javascript. I can't risk changes to the system clock causing security problems, so I don't want to use the Date object.

I have a servertimestamp and I would like to create a timer that is independent of the operating system time to calculate the current server time. I'll use this calculated value to allow different operations during windows on the server time. I'm not concerned about javascript hacking as I know there's no security there, but I would like to prevent the casual user from messing around with the availability of certain features.

Thanks

Added for clarification:

This project relates to live video events. The live video has a start time relative to the server and through another webservice I am able to grab the server time. What I need to do is to maintain this server time on the client machine independently of the user's local time to determine when the live stream should start and stop on the client. So the problem isn't really the format that I store the server time, it's how do I get the elapsed time from when I grabbed the server time without using a date object.

Share Improve this question edited Dec 18, 2011 at 13:05 Shane asked Dec 18, 2011 at 12:46 ShaneShane 5,1615 gold badges41 silver badges55 bronze badges 2
  • I'm not sure why without using a date object is of any significance, but FWIW, my solution does not really use a date object. It merely creates one for initialization. It is turned into a millisecond value immediately, which then is used. You could use a millisecond value from the start, if wanted to. – Tomalak Commented Dec 18, 2011 at 13:10
  • That was incorrectly worded on my part. What I meant to say, was I wanted to avoid using the current time to calculate offset. (e.g. new Date()) – Shane Commented Dec 18, 2011 at 13:18
Add a ment  | 

3 Answers 3

Reset to default 6

You could create your own timer object:

function Timer(init, precision) {
  var start = time = new Date(init || null).valueOf(),
      precision = precision || 100;

  setInterval(function () { time += precision; }, precision);

  this.elapsed = function() { return time - start; };
  this.getDate = function() { return new Date(time); };
}

and use it:

var t = new Timer();    // default timer
/* ... do things ... */
alert( t.elapsed() );   // a millisecond value
alert( t.getDate() );   // a Date object

Use the init parameter to start the timer with any given date value you prefer.

You could have the server send the server time and store it in a javascript Date variable. Then you can base your calculations on this variable knowing that it will be the server time and won't be dependent on the client system clock. As far as executing some javascript method at regular intervals is concerned you could use the setTimeout and setInterval methods.

This function doesn't work well, because it accumulates error from the interval, the interval is not pletely precise it depends on javascript cycles.

function Timer(init, precision) {
  var start = time = new Date(init || null).valueOf(),
      precision = precision || 100;

  // here's where the error acummulates
  setInterval(function () { time += precision; }, precision);

  this.elapsed = function() { return time - start; };
  this.getDate = function() { return new Date(time); };
}

I tested with this script:

const t = new Timer();
setTimeout( () => {
  console.log(t.elapsed());
}, 1000 * 30);

// it returns 28800 (200ms of error, and is accumulative)

My improved versión:

function Timer(init, precision) {
  const baseTime = new Date().valueOf();
  let time = init ? new Date(init).valueOf() : baseTime;
  let delta = baseTime - time;
  const start = time;
    precision = precision || 100;

  const interval = setInterval(() =>  time = (new Date()).valueOf() - delta, precision);

  this.elapsed = () => time - start;
  this.getDate = () => new Date(time);
  this.getTime = () => time;
  this.stop = () => clearInterval(interval);
}

const testTime = 1000 * 60;

const t1 = new Timer();
setTimeout( () => {
  console.log(t1.elapsed(),t1.getDate());
  t1.stop();
}, testTime);
// 59988 Tue Mar 23 2021 11:28:42 GMT-0300 (Chile Summer Time)

const t2 = new Timer('Jan 01, 2020');
setTimeout( () => {
  console.log(t2.elapsed(), t2.getDate());
  t2.stop();
}, testTime);
// 59988 Wed Jan 01 2020 00:00:59 GMT-0300 (Chile Summer Time)

const t3 = new Timer('Jan 01, 2020', 500);
setTimeout( () => {
  console.log(t3.elapsed(),t3.getDate());
  t3.stop();
}, testTime);
// 59937 Wed Jan 01 2020 00:00:59 GMT-0300 (Chile Summer Time)

本文标签: timeJavascript equiv to flashutilsgetTimerStack Overflow