Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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 为什么这个Js/jquery代码使用了70%的CPU?_Javascript_Jquery_Cpu - Fatal编程技术网

Javascript 为什么这个Js/jquery代码使用了70%的CPU?

Javascript 为什么这个Js/jquery代码使用了70%的CPU?,javascript,jquery,cpu,Javascript,Jquery,Cpu,我正在使用下面的代码块来更新我的进度条和其他一些东西。但有点不对劲。当这个页面加载时,我的cpu工作得很疯狂。我在N秒后停止,所以5秒后一切都必须完成。我错了吗 var start = new Date(); var maxTime = 5000; var maxTimeSec = maxTime / 1000; //convert ms to sec : 20000 / 1000 = 20 sec var timeoutVal = Math.floor( maxTime / m

我正在使用下面的代码块来更新我的进度条和其他一些东西。但有点不对劲。当这个页面加载时,我的cpu工作得很疯狂。我在N秒后停止,所以5秒后一切都必须完成。我错了吗

var start      = new Date();
var maxTime    = 5000;
var maxTimeSec = maxTime / 1000; //convert ms to sec : 20000 / 1000 = 20 sec
var timeoutVal = Math.floor( maxTime / maxTimeSec ); //every 1 second

var counter = 0;
var tt      = setInterval(function(){ animateUpdate() },1000);

//Call function
animateUpdate();

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

//send some data to somewhere
function sendStat(){
    var lang  = $("#langCode").attr("data-lang");
    var url   = $("#pageUrl").attr("data-pageUrl");
    var title = $("#pageTitle").attr("data-pageTitle");
    var user  = $("#user").attr("data-user");

    $.ajax({
        type: "POST",
        url: "/actions/setStats.php",
        data: {
            "url"       : url,
            "langCode"  : lang,
            "title"     : title,
            "user"      : user
        },
        success: function(res){
            console.log(res);
        }
    });
}

//My timer
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(counter > maxTimeSec) {
        clearInterval(tt);
        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 :)");
        }
        sendStat();
    } else {
        $('#timerProgress').css("width", perc + "%");
        $('#timerCountdown').text(perc + "%");
        //setTimeout(animateUpdate, timeoutVal);
        counter++;
    }
} 

也许您应该尝试使用
jQuery.animate()
进行回调

例如:

我不相信代码会导致报告的行为。你们能创建一个SSCCE JSFIDLE吗?我敢肯定,除了jquery.js、divs和上面的代码,页面上什么都没有。这不是SSCCE。如图所示,该代码不会导致任何CPU问题。请使用浏览器调试工具查看时间的去向。这就是分析器的用途。在我的chrome浏览器中运行此代码完全不需要CPU。