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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?