将倒数计时器转换为分钟

时间:2014-02-01 11:48:30

标签: javascript jquery

我创建了一个简单的倒数计时器,它会减少输入的总秒数。

http://jsfiddle.net/tmyie/cf3Hd/

但是,我不确定如何使用1:791:78等格式将3分钟(输入的数字)变成秒数。

$('.click').click(function () {
    var rawAmount = $('input').val();
    var cleanAmount = parseInt(rawAmount);
    var totalAmount = cleanAmount * 60
     $('input').val(" ");

    var loop, theFunction = function () {

        totalAmount--;

        if (totalAmount == 0) {

            clearInterval(loop);
        }
        $('p').text(totalAmount);
    };

    var loop  = setInterval(theFunction, 1000);

})

任何帮助都会很棒。

2 个答案:

答案 0 :(得分:3)

这会显示时间2:592:582:57等等...

1:791:78不是有效时间,因为一分钟有60秒。

Here's the fiddle

$('.click').click(function () {
    var rawAmount = $('input').val();
    var cleanAmount = parseInt(rawAmount);
    var totalAmount = cleanAmount * 60;
     $('input').val(" ");

    var loop, theFunction = function () {

        totalAmount--;

        if (totalAmount == 0) {

            clearInterval(loop);
        }
        var minutes = parseInt(totalAmount/60);
        var seconds = parseInt(totalAmount%60);
        if(seconds < 10)
            seconds = "0"+seconds;
        $('p').text(minutes + ":" + seconds);
    };

    var loop  = setInterval(theFunction, 1000);

})

答案 1 :(得分:0)

试试这段代码

$('.click').click(function () {
        var rawAmount = $('input').val().split(':');
        var showTime;
        var cleanAmount = ((parseInt(rawAmount[0])*60) +parseInt(rawAmount[1]));
        $('input').val(" ");
        var loop, theFunction = function () {
            cleanAmount--;
            if (cleanAmount == 0) {
                clearInterval(loop);
            }
           var minutes="0";
             var seconds ="0";
            if(cleanAmount >60){
                minutes = parseInt(cleanAmount/60);
                seconds = parseInt(cleanAmount%60);
            }
            else{
                seconds = cleanAmount;   
                minutes ="0";
            }
                if(seconds<10)
                    seconds = "0"+seconds;
                $('p').text(minutes+':'+seconds);
        };

        var loop  = setInterval(theFunction, 1000);
    });