2015-06-19 52 views
-1

我有一个进度条,我在10秒内将其值增加到0到100%。我想在10秒后停止每个过程。但是当我在下面的代码中取消注释时,我的cpu会接近80% - 90%。我认为100%后仍然有效。停止进度栏10秒后| CPU%80

var start = new Date(); 
    var maxTime = 10000; 
    var maxTimeSec = 10; 
    var timeoutVal = Math.floor(maxTime/100); 
    animateUpdate(); 

    function updateProgress(percentage, second) { 
     $('#timerProgress').css("width", percentage + "%"); 
     $('#timerCountdown').text(percentage + "%" + second + "sec"); 
    } 

    function isLogined(){ 
     userId = $("#userInfo").attr("data-user") ; 
     userId = parseInt(userId); 
     var logined = false; 
     if(userId > 0){ 
      logined = true; 
     } 
     return logined; 
    } 

    function animateUpdate() { 
     var now  = new Date(); 
     var timeDiff = now.getTime() - start.getTime(); 

     var sec  = maxTimeSec - Math.round((timeDiff/maxTime) * maxTimeSec); 
     var perc  = Math.round((timeDiff/maxTime)*100); 
     //console.log(perc); 
     if (perc <= 100) { 
      updateProgress(perc, sec); 
      setTimeout(animateUpdate, timeoutVal); 
     } 
     else{ 

      var bottomDiv = $('#bottomDiv'); 
      bottomDiv.show(); 
      if(isLogined()){ 
       bottomDiv.text("Congratulations. You're lucky to read this article. We've updated your score."); 

      }else{ 
       bottomDiv.text("Congratulations. You're lucky to read this article. If want to count your score you must login :)"); 
      } 

      return true; 
     } 
    } 

有没有在这个代码中的错误?

+0

为什么downvote?我认为问题很清楚。 – hakiko

+0

如果我没有弄错你的'timeoutVal'是100ms –

+0

你可以通过将它放在jQuery的'.animate()'上来解决动画编码中的很多麻烦,但是你的CPU可能仍然会很高。 [这是我的尝试](http://jsfiddle.net/wrctk3at/)。 –

回答

0

无法再现:

https://jsfiddle.net/7jgLLvfz/

铬CPU曲线显示96%空闲,无重CPU的负载。需要更多信息。

var a = "Manditory codeblock so jsfiddle can be linked"