-1

I'm working on a simple countdown timer using Javascript/jQuery.

Basically, I can enter the amount of minutes in the input field and it will convert it to minutes and seconds and starts the countdown.

so, if i enter 30, it will start the countdown from 29 in this format: 29:59

What I need to do is to be able to enter the minutes and seconds in the input field and it should countdown properly but I have no idea how to do this.

for example, I want to enter 26:15 and the countdown should start from that time.

this is my entire code:

HTML:

<h1>Enter minutes you want to work </h1><p>0:00</p>
<input id="request" type="text" value="<?php echo $end_time; ?>">

<input id="time" type="text" value="">
<a href="#" class="click">Click here</a>

jQuery:

<script>
$('.click').click(function () {
    var rawAmount = $('#request').val();
    var cleanAmount = parseInt(rawAmount);
    var totalAmount = cleanAmount * 60;
     $('#request').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);
        $('#time').val(minutes + ":" + seconds);
    };

    var loop  = setInterval(theFunction, 1000);

})
</script>

could someone please advise on this issue?

EDIT: http://jsfiddle.net/71nab0b4/1/

H.HISTORY
  • 520
  • 9
  • 28

2 Answers2

0

You want to break your input string into minutes and seconds, then set your totalAmount to that value like so:

var rawValue = $('#request').val();
var split = rawValue.split(':');

var minutes = split[0];
var seconds = split[1];

var totalAmount = parseInt(minutes, 10) * 60;

if (seconds) {
    totalAmount += parseInt(seconds, 10);
}

however, you will likely want to do some validation.

Alex McMillan
  • 17,096
  • 12
  • 55
  • 88
0

Use split

rawAmount.split(':');

https://jsfiddle.net/533tkxtu/

Alexey Rytikov
  • 558
  • 6
  • 12