2011-04-04 37 views
0

什麼是這個計時器的問題[JS]有什麼問題在此計時器[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

回答

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> 
+0

號()可以避免這些問題。 – cwallenpoole 2011-04-04 20:00:08

2

parseInt("09")返回0,如果你將其更改爲parseFloatparseInt("09", 10)它會奏效。

-1

parseInt()方法實際上返回的第一個整數它找到,因此,你都可以從「09」回0,當你期待9.嘗試在那裏使用parseFloat()方法,你應該是金黃的。它會返回'9',這正是你所期望的。我希望這有幫助!!

+0

它不會像你說的那樣工作,parseInt('09')會返回零,因爲前導零(在大多數瀏覽器中)會將該數字視爲八進制數。由於'09'不存在八進制(不存在9),因此parseInt返回零。將字符串轉換爲數字的更常用方法是一元+,例如x = + t或使用數字作爲函數,例如, x =數字(t) – RobG 2011-04-05 02:58:19

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

+1。聖煙,這幾乎就是我準備發佈的答案。直到添加調用pad功能的toString函數。這是伊利。 – gilly3 2011-04-04 19:57:12

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