jquery启动计时器运行速度非常快

时间:2014-05-27 08:43:23

标签: javascript jquery

我正在使用jQuery函数来播放倒计时器,但是在一些间隔秒之后的某些时候,手的速度非常快,因此结束的时间远远少于定义的时间。

这是我的代码:

function runTimer(){

    total_seconds = 59;
    total_minutes = parseInt($("body").data("exam_time")) - 1;

    examTimer = setInterval(function(){
        $('.countMinutes').find('.digit').html(total_minutes + '<small> mins</small>');
        $('.countSeconds').find('.digit').html(total_seconds + '<small> sec</small>');
        total_seconds = total_seconds - 1;
        if(parseInt(total_seconds) <= 0){
            total_minutes = total_minutes - 1;
            total_seconds = 60;
        }
        if(parseInt(total_minutes) == 5){

            if($("body").data("popUpRemaining") == "true"){
                popUpNot('error','Less than five minutes remaining');
                $("body").data("popUpRemaining","false");
            }
        }
        if(parseInt(total_minutes) <= -1){
            finishExam('timeOver');
            clearInterval(examTimer);
        }
    },1000);
}

2 个答案:

答案 0 :(得分:1)

我希望你在某处运行'runTimer()'函数两次。例如,它位于单击侦听器上并且您单击两次,或者将其绑定为单击侦听器两次,并且只需单击一次就会启动两次。

第二个想法是,由于你的计数器是全局变量,你可能在其他地方使用相同的名称(在其他函数中)。

答案 1 :(得分:1)

如果您不能将其作为样本提供,那么这里的人们很难重现。由于您的声誉不允许链接atm,请将您的代码缩减为一个可以放在这里的简短示例。也许在减少的时候你会自己注意到错误(我做过几次=)

现在你的代码:

  • 使用jQuery缓存,不要每次都重新选择元素。这样可以节省性能
  • 范围变量正确以防止意外修改(例如下面的内容 - 可能无法正常工作!)

    function runTimer(){
    
        var $body = $('body');
        var $min = $('.countMinutes .digit');
        var $sec = $('.countSeconds .digit');
    
        var total_seconds = 59;
        var total_minutes = parseInt($body.data("exam_time")) - 1;
    
        var examTimer = setInterval(function(){
            $min.html(total_minutes + '<small> mins</small>');
            $sec.html(total_seconds + '<small> sec</small>');
            total_seconds = total_seconds - 1;
            if (parseInt(total_seconds) <= 0){
                total_minutes = total_minutes - 1;
                total_seconds = 60;
            }
            if (parseInt(total_minutes) == 5){
                if ($body.data("popUpRemaining") == "true"){
                    popUpNot('error','Less than five minutes remaining');
                    $body.data("popUpRemaining","false");
                }
            }
            if (parseInt(total_minutes) <= -1){
                finishExam('timeOver');
                clearInterval(examTimer);
            }
        },1000);
    
    }
    
  • 不要使用复杂的开关逻辑。在您的情况下,只需使用秒并计算分钟数,这将使其更容易理解并减少错误。

    var body = document.getElementsByTagName('body')[0];
    
    function runTimer(){
    
        var totalSec = 600; // 10 min
        var digitMin = Math.floor(totalSec / 60);
        var digitSec = totalSec % 60;
    
        totalSec++;
        var timer = setInterval(function(){
            totalSec -= 1;
            digitMin = Math.floor(totalSec / 60);
            digitSec = totalSec % 60;
            if (totalSec == 0){
                clearInterval(timer);
            }
    
            body.innerText = digitMin +':'+ digitSec;
        },1000);
    
    }
    runTimer();
    

    <强> http://jsfiddle.net/Lsrxh/