2

i have a working timer which returns a value every second

<?php
$timer = "10:00";
?>

<input type="text" value="<?php echo $timer; ?>" id="timer"/>

<script>
$('.ok').click(function() {
  setInterval(function(){

  var timer=$('#timer').val();
  var time = timer.split(":");
  var timer = (parseInt(time[0])*60) + parseInt(time[1]);
  var remaining= (timer)-1;
  var minutes = parseInt( remaining / 60 ) % 60;
  var seconds = remaining % 60;
  $('#timer').val(minutes+":"+seconds);
}, 1000);
 });
</script>

i want the output to display mm:ss. like if the output is 9 minutes and 1 second, i want it to look like 09:01 not 9:1

Shaunak D
  • 20,588
  • 10
  • 46
  • 79
Christian Burgos
  • 1,572
  • 9
  • 26
  • 48

1 Answers1

5

Try this snippet.

var min = (minutes < 10 ? "0" + minutes : minutes);
var sec = (seconds  < 10 ? "0" + seconds : seconds);
if(remaining >= 0){
    $('#timer').val( min +":"+sec);
}

Or if you need more options try Moment.js

Shaunak D
  • 20,588
  • 10
  • 46
  • 79