10秒后停止进度条| CPU%80

时间:2015-06-19 22:08:29

标签: javascript jquery progress-bar cpu-usage

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

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;
        }
    }

此代码中是否有错误?

1 个答案:

答案 0 :(得分:0)

无法重现:

https://jsfiddle.net/7jgLLvfz/

Chrome CPU配置文件显示96%空闲,没有繁重的CPU负载。需要更多信息。

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