how to get the value of the count timer on button

2019-07-12 18:02发布

<script>
function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
     interval = setInterval(function () {
          minutes = parseInt(timer / 60, 10)
          seconds = parseInt(timer % 60, 10);

          minutes = minutes < 10 ? "0" + minutes : minutes;
          seconds = seconds < 10 ? "0" + seconds : seconds;

          display.text(minutes + ":" + seconds);

         if (--timer < 0) {
            alert("Time has ended");
            $("#registration-form").submit()
            clearInterval(interval);
          }
        }, 1000);
}

 jQuery(function ($) {
    s=10;
    var Minutes = 60 * s,
    isplay = $('#time');
    startTimer(Minutes, display);
});
</script>

<div class="col-md-4">
  <div class="test" >Time Left <span id="time"></span>min</div>
</div>

<form name="frmRegistration" id="registration-form" method="post">
  <input class="btnAction" type="submit" name="finish" id="finish" value="Finish" onclick="clearInterval(interval)" style="display:none;">
</form>

This is my countdown timer when the page loads the timer will start when i click the button i should get the value of how many minutes the timer has run for example if the timer is run for 2 min i should get the value as 2 min and i want it store it in the database i have tried the following but it doesnt work please hlep

1条回答
狗以群分
2楼-- · 2019-07-12 18:42

The easiest and most reliable way would be to store the date the timer started in a variable, then get the difference between that date and the current date when the button is clicked, something like this:

$('button').click(function() {
    var now = new Date();
    var seconds = (now.getTime() - startDate.getTime()) / 1000;
    $('#runtimes').append('<p>' + seconds + '</p>');
});

Working example

This shows the value in seconds, but this can easily be converted to minutes and rounded up or down as you require.

查看更多
登录 后发表回答