将秒转换为时间格式javascript

时间:2014-01-10 12:59:31

标签: javascript jquery ajax

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>

我有这个功能来制作一个倒数计时器..输出数量取决于我放在MAX_COUNTER ..但我想把它转换为hh:mm:ss ...因为我是javascript的新手。如何将其转换为hh:mm:ss格式?帮助.. :(

1 个答案:

答案 0 :(得分:2)

将此代码放入更新功能

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