문제

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