2015-04-02 64 views
0

這是我擁有的代碼。非常混亂,但由於我缺乏經驗,我無法檢測爲什麼它不起作用。根據我的計算,減量是js標準,至少在毫秒,秒和分鐘,不確定小時。嘗試創建重置自身的24小時計時器

這是代碼。提前致謝。

<!DOCTYPE html> 
<html> 
<body> 

<span id="tHours"></span>:<span id="tMins"></span>:<span id="tSeconds"></span>:<span id="tMilli"></span> 

<script> 
    var hours = 1; 
    var mins = hours * 60; 
    var secs = mins * 60; 
    var mill = secs * 100; 
    var currentHours = 0; 
    var currentSeconds = 0; 
    var currentMinutes = 0; 
    vas currentMilli = 0; 
    setTimeout('DecrementMilli()',100); 
    setTimeout('DecrementSeconds()',1000); 
    setTimeout('DecrementMinutes()',10000); 
    setTimeout('DecrementHours()',100000); 

    function DecrementMilli() { 
     currentMilli = secs % 100; 
     if(currentMilli <= 99) currentMilli = "000" + currentMilli; 
     secs--; 
     document.getElementById("tMilli").innerHTML = currentMilli; 
     if(mill !== -1) setTimeout('Decrement()',100); 
    } 
     function DecrementSeconds() { 
     currentSeconds = secs % 60; 
     if(currentSeconds <= 9) currentSeconds = "0" + currentSeconds; 
     secs--; 
     document.getElementById("tSeconds").innerHTML = currentSeconds; 
     if(secs !== -1) setTimeout('Decrement()',1000); 
    } 
     function DecrementMinutes() { 
     currentMinutes = Math.round(secs/60); 
     if(currentMinutes <= 60) currentMinutes = "00"; 
     mins--; 
     document.getElementById("tMins").innerHTML = currentMinutes; 
     if(mins !== -1) setTimeout('Decrement()',10000); 
    } 
     function DecrementHours() { 
     currentHours = Math.round(1440/60); 
     if(currentHours <= 24) currentHours - 1; 
     hours--; 
     document.getElementById("tHours").innerHTML = currentHours; 
     if(hours !== -1) setTimeout('Decrement()',100000); 
    } 
</script> 

</body> 
</html> 
+0

它是有效的顛簸一個線程?如果沒有,對不起,請隨意做你必須做的。 – 2015-04-02 20:19:52

回答

0

嗯,我解決我自己的問題,但因爲它沒有在24:00開始它的不雅: 00但在23:59:59。但這是一個開始。

我會在這裏的情況下它張貼在它可以幫助任何人

<script type="text/javascript"> 
var count = 86400; 
var counter = setInterval(timer, 1000); 

function timer() { 
    count = count - 1; 
    if (count == -1) { 
     clearInterval(counter); 
     return; 
    } 

    var seconds = count % 60; 
    var minutes = Math.floor(count/60); 
    var hours = Math.floor(minutes/60); 
    minutes %= 60; 
    hours %= 60; 

    document.getElementById("timer").innerHTML = hours + ":" + minutes + ":" + seconds; // watch for spelling 
} 
</script> 



<span id='timer'></span> 
0

在你的間隔時間是錯誤的。你可以嘗試下面的代碼。只要把你瓦爾在你的HTML,如:

<span id='tHours'>23</span>:<span id='tMins'>59</span>:<span id='tSeconds'>59</span>:<span id='tMilli'>99</span> 

而像JS:

var milli = 99; 
var sec = 59; 
var min = 59; 
var hour = 23; 

setInterval(function() { 
    milli = milli == 0 ? 99 : milli - 1; 
    $('#tMilli').text(double0(milli)); 
},10); 

setInterval(function() { 
    sec = sec == 0 ? 59 : sec - 1; 
    $('#tSec').text(double0(sec)); 
},1000); 

setInterval(function() { 
    min = min == 0 ? 59 : min - 1; 
    $('#tMin').text(double0(min)); 
},60000); 

setInterval(function() { 
    hour = hour == 0 ? 23 : hour - 1; 
    $('#tHour').text(double0(hour)); 
},1440000); 

function double0 (num) { 
    num = num.toString().length == 1 ? '0' + num : num; 
    return num; 
} 
+0

嗨@Thalsan謝謝你我學會了我的錯誤,我錯誤地使用了錯誤的減法,因爲我忘記了我不再以單個時間單位工作,唉!我是個白癡哈哈。但是,你的櫃檯也不動。我認爲這是因爲你的腳本中的id與你的html不同,但那不是它。謝謝無論如何朋友:) – 2015-04-03 20:31:15