我正在尝试根据番茄工作法制作一个倒数计时器,你工作 25 分钟然后休息一下。我正在使用 jQuery.countdown 和 moment.js 插件在 Javascript 和 jQuery 中编写页面。我有它,所以当用户点击开始按钮时,它会倒计时 25 分钟,但是在计时器完成后,我希望有两个中断选项再次重新启动计时器。然后它只是循环 25 分钟->休息->25 分钟->休息->等,具体取决于用户在倒计时结束时按下的按钮。
倒计时完成后的按钮现在是如何无能为力的。我觉得我需要重置计时器,但是 jQuery.countdown 的文档有点难以通过。
<div class="container-fluid">
<p>start timer and then get to work</p>
<div class="container-fluid">
<div class="container-fluid">
<span id="timer"></span>
</div>
</br>
<div class="containter-fluid" id="timer-buttons">
<span><button type="button" class="btn btn-secondary btn-sm" id="btn-start-timer">start timer</button></span>
</div>
</div>
<script type="text/javascript">
var $timer = $('#timer');
var $timerButtons = $('#timer-buttons');
$('#btn-start-timer').click(function(event) {
// get the current time when button clicked
var currentTime = moment();
// add 25 minutes for interval
var timerInterval = moment(currentTime).add(25.0, 'm');
// format interval
var timerIntervalFormatted = moment(timerInterval).format('YYYY/MM/DD HH:mm:ss');
// start the 25 minute timer and at the end popup two buttons for breaks
$timer.countdown(timerIntervalFormatted, function(event) {
$(this).html(event.strftime('%M:%S'));
}).on('finish.countdown', function() {
$timerButtons.html('<span><button type="button" class="btn btn-secondary btn-sm" id="btn-short-break">short break</button></span> <span><button type="button" class="btn btn-secondary btn-sm" id="btn-long-break">long break</button></span>');
});
});
$('#btn-short-break').click(function(event) {
var currentTime = moment();
var shortBreak = moment(currentTime).add(1.0, 'm');
var shortBreakFormatted = moment(shortBreak).format('YYYY/MM/DD HH:mm:ss');
$timer.countdown(shortBreakFormatted, function(event) {
$(this).html(event.strftime('%-M:%S'));
}).on('finish.countdown', function() {
$timerButtons.html('<span><button type ="button" class="btn btn-secondary btn-sm" id="btn-start-timer">start timer</button></span>');
});
});
$('#btn-long-break').click(function(event) {
var currentTime = moment();
var longBreak = moment(currentTime).add(2.0, 'm');
var longBreakFormatted = moment(longBreak).format('YYYY/MM/DD HH:mm:ss');
$timer.countdown(longBreakFormatted, function(event) {
$(this).html(event.strftime('%-M:%S'));
}).on('finish.countdown', function() {
$timerButtons.html('<span><button type ="button" class="btn btn-secondary btn-sm" id="btn-start-timer">start timer</button></span>');
});
});
</script>