这个计时器有什么问题[JS]

时间:2011-04-04 19:17:31

标签: javascript time timer setinterval

此计时器[JS]

有什么问题
<script type="text/javascript">

    var remain = "14:14";
    setInterval ("timer()", 660);

    function timer(){

        var remainM = parseInt(remain.split(":")[0]);
        var remainS = parseInt(remain.split(":")[1]);

        //document.getElementById("hello").innerHTML = parseInt(remainS);

        if (remainS==0) {
            if (remainM==0) {

            } else {
                remainM = parseInt(remainM) - 1;
                remainS = 59;
            }
        } else {
            remainS = parseInt(remainS) - 1;
        }

        var remainSr = String(remainS);
        var remainMr = String(remainM);

        if (parseInt(remainS)<=9) {remainSr = "0" + String(remainS);}
        if (parseInt(remainM)<=9) {remainMr = "0" + String(remainM);}

        remain = String(remainMr) + ":" + String(remainSr);
        document.getElementById("hello").innerHTML = remain;

    }

</script>

它从14:09跳到13:59

6 个答案:

答案 0 :(得分:3)

忽略parseInt的第二个参数是历史错误。

查看此文章:http://www.devguru.com/technologies/ecmascript/quickref/parseint.html

试试这个版本:

<script type="text/javascript">
var remain = "14:14";
setInterval("timer()", 660);

function timer() {
    var remainM = parseInt(remain.split(":")[0], 10);
    var remainS = parseInt(remain.split(":")[1], 10); 
    if (remainS == 0) {
        if (remainM == 0) {} else {
            remainM = parseInt(remainM) - 1;
            remainS = 59;
        }
    } else {
        remainS = parseInt(remainS) - 1;
    }
    var remainSr = String(remainS);
    var remainMr = String(remainM);
    if (parseInt(remainS) <= 9) {
        remainSr = "0" + String(remainS);
    }
    if (parseInt(remainM) <= 9) {
        remainMr = "0" + String(remainM);
    }
    remain = String(remainMr) + ":" + String(remainSr);
    document.getElementById("hello").innerHTML = remain;
}
</script> 

答案 1 :(得分:2)

您需要在parseInt中指定基数。如果执行parseInt("09"),则要求解析八进制数9,这是一个错误。请参阅parseInt documentation on MDC

答案 2 :(得分:2)

parseInt("09")如果您将其更改为parseFloatparseInt("09", 10),则会返回0.

答案 3 :(得分:1)

其他人已经解决了当前的问题 - 您不小心将数字视为八进制而不是十进制 - 但更深层次的问题是您将数字存储为字符串。不断转换为字符串和从字符串转换是错综复杂且容易出错的。更好的设计是将时间存储为两个数字,并具有将这些数字按需转换为字符串的功能。这是一个将数字和字符串表示分开的版本:

var remain = {
    mins: 14, 
    secs: 14,
    toString: function () {
        var pad = function(num) { return (num < 10 ? "0" : "") + num; };
        return pad(this.mins) + ":" + pad(this.secs);
    }
};

setInterval (timer, 660);

function timer() {
    if (remain.secs === 0) {
        if (remain.mins > 0) {
            remain.mins -= 1;
            remain.secs = 59;
        }
    } else {
        remain.secs -= 1;
    }
    document.document.getElementById("hello").innerHTML = remain;
}

答案 4 :(得分:-1)

parseInt()方法实际上返回它找到的第一个整数,因此当你期望9时,你从'09'返回0.尝试在那里使用parseFloat()方法,你应该是金色。它将返回'9',这正是您所期待的。我希望这有帮助!!

答案 5 :(得分:-1)

<!DOCTYPE html>
<html>
<body>
<button onclick="Timer()">Try it</button>

<script>
function Timer() {
    var sec=0;
    var mm =0;
    setInterval(function(){
            if(sec<10){
               if(mm>=10) {
                  document.getElementById('p1').innerHTML= mm + ":"+ "0"+sec;
               }
               else {
                                  document.getElementById('p1').innerHTML= "0"+mm + ":"+ "0"+sec;
                               }
                               sec = sec+1;                             
            }
            else if(sec >=10 && sec <= 59) {
               if(mm>=10) {
                  document.getElementById('p1').innerHTML= mm + ":"+ sec;
               }
               else {
                                  document.getElementById('p1').innerHTML= "0"+mm + ":"+ sec;
                               } 
                               sec = sec+1;
                            }
                            else if(sec==60) {
                                     sec=0;
                                     mm = mm+1;
                                     if(mm>=10) {
                                        document.getElementById('p1').innerHTML= mm + ":"+ "0"+sec;
                                     }
                                     else {
                                        document.getElementById('p1').innerHTML= "0"+mm + ":"+ "0"+sec;
                                     }
                               sec = sec+1;            
                            }
                },1000);
}
</script>
<p id="p1"></p>
</body>
</html>