如何为番茄钟添加分钟数
How can I add minutes to a pomodoro clock?
我是一个JS新手,试图用最少的功能构建番茄钟。虽然我有基本的倒计时工作,但我在实现计时器的"添加额外分钟"功能时遇到了困难。
到目前为止,我在变量声明之后立即粘贴了以下内容:
// Add more minutes to timer
add.addEventListener("click", addMin);
function addMin() {
mins += 1;
timer.textContent = mins + ":" + "0" + currentSecs;
}
虽然这会添加倒计时前的分钟数,但它不会更新实际的倒计时分钟数。我哪里做错了?在这个问题上有一双新鲜的眼睛将不胜感激(并拯救几只小猫......
我在下面添加了 HTML 和 JS 代码:
var timer = document.getElementById("timer"),
button = document.getElementById("button"),
add = document.getElementById("add"),
minus = document.getElementById("minus"),
numOfPomodoros = 0;
// Variables for the Countdown to Work:
var mins = 25,
secs = mins * 60,
currentSecs = 0,
currentMins = 0,
comingOffBreak = false;
button.addEventListener("click", startCountdown);
function startCountdown() {
setTimeout(countdown, 500);
}
function countdown() {
currentMins = Math.floor(secs / 60);
currentSecs = secs % 60;
if (currentSecs <= 9) {
// Add an extra zero to the seconds in order to make it look more like a clock
currentSecs = "0" + currentSecs;
}
secs--;
timer.textContent = currentMins + ":" + currentSecs;
if (secs !== -1) {
// Continue the countdown
setTimeout(countdown, 1000);
} else {
// When countdown reaches 0...
if (!comingOffBreak) {
// If user just finished a pomodoro...
countNumOfPomodoros();
if (numOfPomodoros % 4 === 0) {
button.textContent = "Start Long Break";
resetForLongBreak();
} else {
button.textContent = "Start Short Break";
resetForShortBreak();
};
} else {
// If user just finished a break...
button.textContent = "Start Pomodoro";
resetCountdown();
};
};
}
function countNumOfPomodoros() {
// Add 1 to numOfPomodoros after each pomodoro is completed
numOfPomodoros++;
return numOfPomodoros;
}
function resetCountdown() {
// For new pomodoro
mins = 25;
secs = mins * 60;
currentSecs = 0;
currentMins = 0;
comingOffBreak = false;
}
function resetForShortBreak() {
mins = 5;
secs = mins * 60;
currentSecs = 0;
currentMins = 0;
comingOffBreak = true;
}
function resetForLongBreak() {
mins = 30;
secs = mins * 60;
currentSecs = 0;
currentMins = 0;
comingOffBreak = true;
}
<div class="container text-center">
<h1 id="title">POMODORO CLOCK</h1>
<div id="timerBox">
<span id="timer">25:00</span>
</div>
<br>
<button id="minus" class="btn btn-danger">- Minute </button>
<button id="button" class="btn btn-success">Start Pomodoro</button>
<button id="add" class="btn btn-primary">+ Minute</button>
</div>
您正在更新方法和文本中的mins
变量,而不是作为倒计时方法中考虑的变量的秒数。
我还会考虑将其重构如下:
- 有一个变量与番茄钟的开头,
- 有另一个变量,分钟数增加,
- 然后使用以下伪表达式获取要显示的剩余秒数,而不使用倒计时更新变量:
25 * 60 + extraMins * 60 - <current-time-in-secs> + <start-time-in-secs>
相关文章:
- 使用 javascript 将指定的分钟数添加到时间
- 在 Node.js 中向 javascript Date 添加分钟数
- 检查本地存储中的记录是否在 1 分钟前添加
- 使用 JavaScript 给 hh:mm 添加一分钟
- 全日历轴格式添加 2 分钟
- 如何添加提交表单的 5 分钟脚本倒数计时器
- 将分钟添加到 while 循环
- 如何为番茄钟添加分钟数
- 将分钟或秒添加到 Javascript 倒数计时器
- 如何在 JavaScript 中向 Datetime 字符串添加分钟数
- 向 js 日期对象添加分钟时出错
- 将30分钟添加到Date会使其倒退30分钟
- JavaScript倒计时,添加小时数&分钟
- js添加7分钟直到10小时(00:07:04:14:21:)
- JS计时器只适用于秒,如何添加分钟和小时
- 向日期(javascript)添加分钟无效日期
- Javascript在循环中添加分钟数
- 通过使用时刻javascript我只需要添加小时(不增加或添加分钟)/轮到最近的小时或月或年
- JavaScript:解析时间字符串并向其添加分钟数
- Javascript日期和添加分钟