2014-01-10 77 views
1

JavaScript函數秒轉換時間格式的JavaScript

<script type="text/javascript">       
     var MAX_COUNTER = 100; 
    var counter = null; 
    var counter_interval = null; 

    function updateCounter() { 
     var msg = ''; 
     if (counter > 0) { 
      counter -= 1; 
      msg = counter; 
      setCookie('counter', counter, 1); 
     } 
     else { 
      msg = "Counting finished."; 
      stopCounter(); 
     } 
     var el = document.getElementById('counter'); 
     if (el) { 
      el.innerHTML = msg; 
     } 
    } 
    function resetCounter() { 
     counter = MAX_COUNTER; 
    } 
    function stopCounter() { 
     window.clearInterval(counter_interval); 
     deleteCookie('counter'); 
    } 
    function startCounter() { 
     stopCounter(); 
     counter_interval = window.setInterval(updateCounter, 1000); 
    } 
    </script> 

我有這樣的功能,使倒計時timer..it輸出的數量取決於什麼我穿上MAX_COUNTER..but我想轉換它以hh:mm:ss ...因爲我是一個新手在JavaScript。如何將其轉換爲hh:mm:ss格式? PLSS幫助.. :(

回答

2

將這個代碼到你的更新功能

var min=parseInt(MAX_COUNTER/60); 
var sec=MAX_COUNTER%60; 
var hours=parseInt(min/60); 
+0

是MAX_COUNTER或計數器只?的xD 因爲我updateCounter將在resetCounter計數器使用來自MAX_COUNTER function..xD – Zurreal

+0

這是計數器,倒計時值(以秒爲單位) –