增量为两位小数的无限计数器
Infinite counter with increment at two decimals
正在尝试创建一个从 10.41 开始并每秒增加 10.41 的无限计数器。看过各种教程,但我找不到可以解释每秒十进制数单位 (10.41) 增加的教程。这是我使用的代码:
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 += 10.41
而不是 i++
。我还将初始 i
值更改为零,因为您希望它从 10.41
和计时器开始,而不是 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);
.
将其截断为小数点后两位
我愿意:
function Incro(increment, interval, execFunc){
let iv;
this.increment = increment; this.interval = interval; this.execFunc = execFunc; this.sum = 0;
this.start = ()=>{
iv = setInterval(()=>{
this.sum += this.increment; this.execFunc();
}, this.interval);
return this;
}
this.stop = ()=>{
clearInterval(iv); iv = undefined;
}
}
const incro = new Incro(10.41, 1000, function(){
console.clear(); console.log(this.sum.toFixed(2));
if(this.sum > 100)this.stop();
});
console.log('0.00');
incro.start();
setTimeout(()=>{
incro.stop();
setTimeout(()=>{
incro.start();
}, 2000);
}, 5150);
如您所见JavaScript时间不准确。
正在尝试创建一个从 10.41 开始并每秒增加 10.41 的无限计数器。看过各种教程,但我找不到可以解释每秒十进制数单位 (10.41) 增加的教程。这是我使用的代码:
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 += 10.41
而不是 i++
。我还将初始 i
值更改为零,因为您希望它从 10.41
和计时器开始,而不是 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);
.
我愿意:
function Incro(increment, interval, execFunc){
let iv;
this.increment = increment; this.interval = interval; this.execFunc = execFunc; this.sum = 0;
this.start = ()=>{
iv = setInterval(()=>{
this.sum += this.increment; this.execFunc();
}, this.interval);
return this;
}
this.stop = ()=>{
clearInterval(iv); iv = undefined;
}
}
const incro = new Incro(10.41, 1000, function(){
console.clear(); console.log(this.sum.toFixed(2));
if(this.sum > 100)this.stop();
});
console.log('0.00');
incro.start();
setTimeout(()=>{
incro.stop();
setTimeout(()=>{
incro.start();
}, 2000);
}, 5150);
如您所见JavaScript时间不准确。