Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在5秒内从0增加到一个值_Javascript_Html - Fatal编程技术网

Javascript 在5秒内从0增加到一个值

Javascript 在5秒内从0增加到一个值,javascript,html,Javascript,Html,我需要在5秒内将值从0增加到103551。下面是我使用的逻辑。但它并没有在5秒内增加到所需的值 var counter = 0; var el = document.getElementById('seconds-counter'); function incrementSeconds() { counter += 1; el.innerText = "Processing " + counter + " execution records"; if(counter =

我需要在5秒内将值从0增加到103551。下面是我使用的逻辑。但它并没有在5秒内增加到所需的值

var counter = 0;
var el = document.getElementById('seconds-counter');

function incrementSeconds() {
    counter += 1;
    el.innerText = "Processing " + counter + " execution records";
    if(counter == 103551) {
        console.log(new Date());
    }
}

console.log(new Date());
var time = 103551/5000;
var cancel = setInterval(incrementSeconds, time);
HTML


你的数学有点不对劲。103551/5000=20.7102ms

1000ms=1s,5s=5000ms,5000/20.7102=240次迭代

您要求解的方程是5000/x=numIterations

因此x=5000/单位


注意:大多数浏览器都有一个可以在setInterval()中设置的最小值,因此每个循环可能需要增加1以上,才能在5秒内计数到103551。因为这是一个奇怪的特殊问题,所以我想问一下,这可能是一个作业,所以我会从这个答案中省略一个完整的解决方案。祝你好运

只是出于好奇-目的是什么,为什么你不能说,将var设置为103551,超时5秒,例如Op可能想显示随着时间的推移计数的数字。我已将逻辑更改为
var iter=103551/5000;var时间=5000/itervar iter=5000/103551
var time=5000/103551
var time=5000/103551
的值大约是
0.04828538594508986
并且计数器仍然没有在5秒内完成。你读过我的整个响应了吗?SetInterval的值为0.048毫秒时不起作用。您需要计算一下定时函数的增量。根据大多数浏览器的说法,平均每15毫秒只能触发一次定时函数,如果每次仅增量1,您的计数甚至不会超过500。
<div id='seconds-counter'> </div>