无法在Float32Array

时间:2015-10-30 18:06:00

标签: javascript arrays google-chrome date

在Float32Array中,日期只能设置为一次。进一步的更新没有区别。运行下面的代码段并查看(Chrome,Firefox,IE)。

有解决方法吗?我必须将当前时间作为Float32Array的值之一。



var d1 = document.querySelector('#d1');
var d2 = document.querySelector('#d2');
var d3 = document.querySelector('#d3');

var fa = new Float32Array(1);
var date;
setInterval(function() {
  date = Date.now();
  fa[0] = 52343225; // another value to clear the previous value - doesn't help
  fa[0] = date;

  d1.textContent = date;
  d2.textContent = fa[0];
  d3.textContent = date - fa[0];

}, 50);

<div>Date: <span id="d1"></span>
</div>
<div>Float32Array value: <span id="d2"></span>
</div>
<div>Difference: <span id="d3"></span>
</div>

<p>As you can see above the date in Float32Array is not changing. Initially the date is set in the FUTURE</p>
&#13;
&#13;
&#13;

new Date().getTime() // gives the same result
performance.now() // works correctly

有趣的是,日期总是在打开标签后的一分钟内设置。即使页面刷新也没有重置此计时器。只打开一个新选项卡并运行该脚本才会开始倒计时。

1 个答案:

答案 0 :(得分:1)

似乎32位浮点数无法正确处理日期值。我切换到了一个正常工作的float64。

var d1 = document.querySelector('#d1');
var d2 = document.querySelector('#d2');
var d3 = document.querySelector('#d3');

// CHANGED HERE
var fa = new Float64Array(1);
var date;
setInterval(function() {
  date = Date.now();
  fa[0] = 52343225; // another value to clear the previous value - doesn't help
  fa[0] = date;

  d1.textContent = date;
  d2.textContent = fa[0];
  d3.textContent = date - fa[0];

}, 50);
<div>Date: <span id="d1"></span>
</div>
<div>Float32Array value: <span id="d2"></span>
</div>
<div>Difference: <span id="d3"></span>
</div>