سؤال

Would event.timeStamp and Date.getTime() return same values if taken at the exact same time?

For instance, could I use getTime() in order to calculate how much time passed since an event occurred, or might it happen that the 2 use different epoch / references?

هل كانت مفيدة؟

المحلول

No, they're always on the same reference, that is UTC, which is the time zone of all date internal storages.

You can also use Date.now().

نصائح أخرى

It depends on the browser.

Using the following HTML document I got different results using different browsers:

<!DOCTYPE html>
<html>
<body>

<div id="foo">onclick delay:</div>
<button onclick='var n=document.getElementById("foo"); n.innerHTML="onclick delay: "+((new Date).getTime()-event.timeStamp)+"ms"'>Click</button>

</body>
</html>

The delay printed on screen is:

Firefox: large number

IE: -4ms .. 0ms

Chrome: 0ms .. 2ms

Opera: 0ms .. 2ms

=> You cannot really use it if your code should work in different browsers.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top