简体   繁体   English

JavaScript setInterval函数不会停止

[英]Javascript setInterval function doesn't stop

I am new to javascript. 我是javascript新手。 I am checking a time period from my database and showing how much time left to the end of the period. 我正在从数据库中检查一个时间段,并显示该时间段还剩多少时间。

I got the time value with strtotime as a variable $lefttime in php. 我在strtotime中将时间值作为变量$lefttime获得。 My problem is with javascript part so i am not writing the php codes. 我的问题是JavaScript部分,所以我没有写PHP代码。

So my javascript code is: 所以我的JavaScript代码是:

var s = <?php echo $lefttime; ?>; //s stands for seconds
var d = Math.floor(s/60);         //d stands for minutes
s -= d*60;

var count = setInterval(functime, 1000);
    function functime() {
        s--;

        if(s < 0) {
            d--;
            s = 59;                 
            var san = s;
            var dak = "0" + d;   
        }
        else if(d < 0 ) {
                clearInterval(functime);
                document.getElementById('id').innerHTML = "over";
            }
        else if(d < 10){
            var dak = "0" + d;
            if(s < 10) { 
                       var san = "0" + s;
                    }
            else { 
                       var san = s; 
                    }
        }
        else if(s < 10){
            var san = "0" + s;
            if(d < 10) { 
                       var dak = "0" + d;
                    }
            else { 
                       var dak = d; 
                    }
        }
        else { 
            var san = s;
            var dak = d;
        }

        document.getElementById('id').innerHTML = dak+":"+san;
    }

I create dak and san variables to check if the value is a digit, if so add 0 to the left of them. 我创建了daksan变量,以检查该值是否为数字,如果是,则在其左侧添加0

So it works perfect except stopping when d < 0 . 因此,除了在d < 0时停止,它的工作原理是完美的。 It counts down till 00:00 and then shows 0-1:59 , and then undefined:undefined . 它倒计时到00:00 ,然后显示0-1:59 ,然后显示undefined:undefined

Please help me I can't find what is wrong. 请帮助我,我找不到问题所在。

setInterval returns an identifier for the interval setInterval返回间隔的标识符

var myInterval = setInterval(functime, 1000);

And it's this numeric identifier that should be passed as a parameter to clearInterval 而且应该将此数字标识符作为参数传递给clearInterval

clearInterval(myInterval);

update: You code is much too complex. 更新:您的代码太复杂了。 You have too many nested if/else, so it makes it complicated to understand. 如果您嵌套的if / else太多,那么它会使理解变得很复杂。 You should create several simple functions to isolate small chunks of code with a specific responsibility. 您应该创建几个简单的函数来隔离特定职责的小代码块。 By example: 例如:

var s = <?php echo $lefttime; ?>; //s stands for seconds
var d = Math.floor(s/60);         //d stands for minutes
s -= d * 60;

update(d, s);
if(d >= 0) {
    var myInterval = setInterval(functime, 1000);
} 
else {
    finish();
}

function functime() {
    s--;
    if(s < 0 ) {
        d--;
        if (d < 0 ) {
            clearInterval(myInterval);
            finish();
            return;
        }
        else {
            s = 59;
        }
    }
    update(d, s);   
}

function format(value) {
    if(value < 10) {
        return '0' + value;
    }
    return value;
}

function update(d, s) {
    var san = format(s);
    var dak = format(d);
    document.getElementById('id').innerHTML = dak + ":" + san;
}

function finish() {
    document.getElementById('id').innerHTML = "over";
}

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

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