doufeng9567 2016-02-25 10:15
浏览 43
已采纳

如何在按钮点击时获取计数计时器的值?

<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条回答 默认 最新

  • duanlie3187 2016-02-25 10:26
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机