Firefox 中的日期对象总是 returns 毫秒四舍五入到百

Date object in Firefox always returns milliseconds rounded to hundreds

最近使用console.time时无意中发现了这个行为。在 Firefox 中,它总是 returns 0ms 或 100ms。发生这种情况是因为日期总是四舍五入到数百毫秒。例如 +new Date() 将 return 1552469978800 而不是 1552469978877。你知道这是从什么时候开始的吗?或者我怎么可能得到准确的时间?也影响 setTimeoutsetInterval.

This happens because the date is always rounded to hundreds of milliseconds.

我在 *nix 上的 Firefox v65 以及 Windows 上的 v48、v56、v57 或 v65 中都没有看到这种行为。

但如果它发生在某些版本或某些平台上,则可能是对 Spectre. For the same reason, the alternative I would have pointed out to (performance.now) 的回应不如其他情况下有用,因为:

The timestamp is not actually high-resolution. To mitigate security threats such as Spectre, browsers currently round the results to varying degrees. (Firefox started rounding to 1 millisecond in Firefox 60.) Some browsers may also slightly randomize the timestamp. The precision may improve again in future releases; browser developers are still investigating these timing attacks and how best to mitigate them.

试试这个:

function convert( ms ) {
    var seconds = ms / 1000;
    var hours = parseInt( seconds / 3600 );
    seconds = seconds % 3600;
    var minutes = parseInt( seconds / 60 );
    seconds = seconds % 60;
    var pad = function(x) { return (x < 10) ? "0"+x : x; }
    return pad(hours)+":"+
    pad(minutes)+":"+
    pad(seconds);
}
var time = 100000000;
console.log(convert(time));

这会将毫秒转换为小时:分钟:秒格式。

终于找到了这个问题的最终答案。整个问题是 privacy.resistFingerprinting 设置在最新版本的 Firefox 中默认启用。

在这种情况下,指纹保护可能会带来更多问题。您现在完全无法在 Javascript 中正确设置时区,因此某些网络应用程序(例如 Slack)将始终显示 GMT+0 时间而不是您的实际时间。

另一个烦人的事情是 JavaScript 使用 setIntervalsetTimeout 函数的动画(特别是影响 jQuery 插件)现在 运行 在 10每秒帧数。

禁用指纹保护后,重启浏览器后一切正常。