<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>
这是我的倒计时器,当页面加载时我会点击按钮启动计时器我应该得到计时器运行了多少分钟的值,例如如果计时器运行2分钟我应该得到的值2分钟,我希望它存储在数据库中我尝试了以下但它不起作用请hlep
答案 0 :(得分:2)
最简单,最可靠的方法是将计时器开始的日期存储在变量中,然后获取该日期与单击按钮时的当前日期之间的差异,如下所示:
$('button').click(function() {
var now = new Date();
var seconds = (now.getTime() - startDate.getTime()) / 1000;
$('#runtimes').append('<p>' + seconds + '</p>');
});
显示以秒为单位的值,但这可以很容易地转换为分钟数,并根据需要向上或向下舍入。