简体   繁体   English

setTimeout的计时问题

[英]Timing issue with setTimeout

The code below demonstrates the issue I am having. 下面的代码演示了我遇到的问题。

When the the js executes, the progress bar fills, as expected rapidly until the max has been reached. 当js执行时,进度条会按预期快速填充,直到达到最大值。

However, the span#pbarval container is updated very slowly and completes LONG after the progress bar has finished. 但是,span #pbarval容器更新非常缓慢,并在进度条完成后完成LONG。

$(document).ready(function () {
    var max= 20000,
        cur=0;
    function updatePBar(){
        cur++;
        jQuery("#pbar").val(cur);
        jQuery("#pbarval").html("" + Math.ceil(cur*100/max) + "%");
        if (cur<=max){
            setTimeout(updatePBar, 10);
        }
    }
    updatePBar();
});

You can see the code executing here: http://jsfiddle.net/EricBrian/fhtp6rpf/ 您可以在此处看到执行的代码: http//jsfiddle.net/EricBrian/fhtp6rpf/

Can somebody explain why this is? 有人可以解释为什么会这样吗? How to make it keep up with the progress bar? 如何让它跟上进度条?

Also, I noticed that if I switch tabs, the setTimeout seems to pause. 另外,我注意到如果我切换标签,setTimeout似乎暂停。 The percentage does not update until I switch back to the tab it is running in. 在我切换回正在运行的选项卡之前,百分比不会更新。

Thanks! 谢谢! -e -e

You're using cur for the value of the progress bar, so the progress bar is full when cur === 100 , but you're displaying cur*100/max as the percentage which doesn't reach 100% until cur == 20000 . 你正在使用cur作为进度条的值,所以当cur === 100时进度条已满,但你显示cur*100/max为百分比,直到cur == 20000才达到100% cur == 20000

You should use the same formula cur*100/max for both, and since you want the quick speed you should also divide your max by 100: 你应该使用相同的公式cur*100/max ,因为你想要快速,你也应该将你的最大值除以100:

http://jsfiddle.net/Paulpro/fhtp6rpf/2/ http://jsfiddle.net/Paulpro/fhtp6rpf/2/

$(document).ready(function () {
    var max= 200,
        cur=0;
    function updatePBar(){
        cur++;
        jQuery("#pbar").val(cur*100/max);
        jQuery("#pbarval").html("" + Math.ceil(cur*100/max) + "%");
        if (cur<max){
            setTimeout(updatePBar, 10);
        }
    }
    updatePBar();
});

I also changed the test cur<=max to cur<max since you probably do not mean to increment cur an extra time when it is already at max . 我也改变了测试cur<=maxcur<max ,因为你可能不意味着递增cur额外的时间,当它已经是max

Your progress bar max is 100 but your javascript variable max is 2000. 您的进度条最大值为100,但您的javascript变量max为2000。

As such, the progress bar fills much quicker because it reaches it right away. 因此,进度条填充速度更快,因为它立即到达它。

This particular expression is the one guilty for it: 这个特殊的表达是有罪的:

Math.ceil(cur*100/max)

Looks like below expression is taking too long. 看起来下面的表达花了太长时间。

Math.ceil(cur*100/max) 

Simply replace that expression with 只需将表达式替换为

cur

and see how it flies. 并看看它是如何飞行的。

This is an answer to part of your question. 这是对您问题的部分答案。 setTimeout is designed to be active when the tab is active. setTimeout设计为在选项卡处于活动状态时处于活动状态。 So the behavior regarding the progress bar not being updated when the tab is no active is normal. 因此,当选项卡未激活时,有关进度条的更新行为是正常的。 You can over come this using this method here . 您可以使用此方法在此来这里

It's just wrong at your cur 你的cur是错的

$(document).ready(function () {
     var max= 20000,
         cur=0;

     function updatePBar(){
         cur++;
         var value = Math.ceil((cur/max) * 100);
         jQuery("#pbar").val(value);
         console.log(cur)
         jQuery("#pbarval").html("" + value + "%");
         if (cur<=max){
             setTimeout(function(){updatePBar();}, 10);
         }
     }
     updatePBar();
});

You are assigning different value to the value of the progress and to the percentage. 您要为进度值和百分比分配不同的值。

Just reuse the same value: 只需重用相同的值:

 var max = 20000, cur = 0; (function updatePBar() { pbarval.innerHTML = (pbar.value = Math.ceil(++cur * 100 / max)) + "%"; if (cur < max) setTimeout(updatePBar, 10); })(); 
 <progress id="pbar" value="0" max="100"></progress> <span id="pbarval"></span> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM