Firefox 中基于 JavaScript 时间的分数

标签 javascript google-chrome date firefox gettime

所以我遇到了这个问题,

对于每秒增加一的分数函数,它可以在 Google Chrome(版本 53.0.2785.116 m)、Microsoft Edge(25.10586.0.0)中运行,但在 Firefox(版本 49.0.1)中不起作用。这可能是由于日期格式的差异造成的吗?

下面的代码是我基于时间的 Canvas 游戏得分。该函数获取从执行开始算起的日期,每秒加1。

关键是分数从 0 开始,每秒增加 1,在浏览器中达到 100。

<小时/>

正在寻找任何能够在 Firefox 上实现此功能的解决方案 - 目前“分数”显示为静态且不会向上计数,这与 Google Chrome 和 Edge 不同。

有什么想法吗? - JS 新手。 预先感谢您的帮助。

var start = new Date().getTime(),
  score = '0.1';

var interval = window.setInterval(function() {
  var time = new Date().getTime() - start;
  
  score = Math.floor(time / 1000);
  
  if(score === 100) { 
    window.clearInterval(interval); 
    if(!alert("You win!\nPress 'OK' to play again")){
        window.location.reload();
    } 
  }

  document.getElementById('displayScore').innerHTML = score += '.00 Score';
});
<div id="displayScore"></div>

最佳答案

您需要在 setIntetval 中传递延迟,因为如果您不这样做,Firefox 会假定默认值为 10(毫秒)

delay The time, in milliseconds (thousandths of a second), the timer should delay in between executions of the specified function or code. If this parameter is less than 10, a value of 10 is used.

这使得它每 10 毫秒运行一次,这是一个非常低的值,因此当您进行像 score = Math.floor(time/1000); 这样的计算时,结果值接近于零,并且您的 var 永远不会增加

关于Firefox 中基于 JavaScript 时间的分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39813241/

相关文章:

html - 图片之间有多余的空白吗?

css - 文本溢出 : ellipsis jumps behind text on Android

google-chrome - 在 Google Chrome 中显示表情符号

date - 在erlang中将时间戳转换为日期时间

javascript - 在 NodeJs 中,为什么新的 Date 构造函数将默认时间设置为上午 7 点?

javascript - 防止在 React 中显示原始 HTML

javascript - Angular 缩小构建后如何修复注入(inject)器错误?

带有 x 轴作为字符串的 Javascript FLOT 组合条形图和折线图

javascript - 为什么我的 highchart 代码不起作用?

javascript - 使用矩以编程方式设置 ionic /Angular 日期输入