无限计数器,以两位小数递增 [英] Infinite counter with increment at two decimals

查看:51
本文介绍了无限计数器,以两位小数递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建一个从10.41开始并每秒增加10.41的无限计数器.看过各种教程,但我找不到一个每秒可以增加十进制数单位(10.41)的教程.这是我正在使用的代码:

Trying to create an infinite counter that starts at 10.41 and increases by 10.41 every second. Have looked at various tutorials but I cannot find one that will account for the increase with a decimal number unit (10.41) every second. This is the code I am using:

setTimeout(start, 0);

var i = 100;
var num = document.getElementById('number');

function start() {
  setInterval(increase, 100);
}

function increase() {
    if (i < 100000) {
      i++;
      num.innerText = i;
    }
}

推荐答案

如果我正确理解,您所要做的就是代替 i ++ ,而需要使用 i + = 10.41.我还已将初始 i 值更改为零,因为您希望它从 10.41 开始并将计时器设置为 1000 ,而不是 100 :

If I understand correctly, all you need to do is, instead of i++ you need to use i += 10.41. I have also changed the initial i value to zero because you want it to start from 10.41 and timer to 1000 instead of 100:

setTimeout(start, 0);

var i = 0;
var num = document.getElementById('number');

function start() {
  increase();
  setInterval(increase, 1000);
}

function increase() {
  if (i < 100000) {
    i += 10.41;
    num.innerText = i.toFixed(2);
  }
}

<span id="number"></span>

此外,我使用 i.toFixed(2); 将其截短至两位小数.

Also, I truncated it off to two decimal places using i.toFixed(2);.

这篇关于无限计数器,以两位小数递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆