user1469270
user1469270

Reputation:

Converting a countdown timer into minutes

I have created a simple countdown timer, that counts down the total seconds inputted.

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

However, I am unsure how to turn 3 minutes (the number entered) into seconds with a format like 1:79, 1:78, etc.

$('.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);

})

Any help would be great.

Upvotes: 1

Views: 589

Answers (2)

Manoj
Manoj

Reputation: 56

try this code

$('.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);
    });

Upvotes: 0

Hugo Sousa
Hugo Sousa

Reputation: 1926

This will show the time like 2:59, 2:58, 2:57, and so on...

1:79, 1:78 isn't a valid time, since a minute has 60 seconds.

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

})

Upvotes: 3

Related Questions