Pomodoro时钟中断按钮不工作

Pomodoro Clock Break Buttons not working

本文关键字:工作 按钮 中断 时钟 Pomodoro      更新时间:2023-09-26

我正在构建一个Pomodoro时钟来改进我的JavaScript,但我遇到了一些问题。对于启动器,中断的递增/递减按钮不会更新显示中的时间。其次,会话时间不会在第一个休息时间段后重新启动。有人能告诉我哪里出了问题吗?

HTML:

<html>
<head>
  <title>David Hall - Pomodoro Clock Zipline</title>
  <link href="https://fonts.googleapis.com/css?family=Orbitron" rel="stylesheet" type="text/css" />
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>
  <div class="container">
    <h1 class="center">Pomodoro Clock</h1>
    <div class="row">
      <div class="center">
      <div class="col-sm-6">Break Length</div>
      <div class="col-sm-6">Work Length</div>
      <div class="col-sm-6" id="center">
        <div class="btn-group">
          <button type="button" class="btn btn-success" id="decreaseBreak"><span class="glyphicon glyphicon-menu-left"></span>
            <button type="button" class="btn btn-success"><span id="breakTime">5</span>
              <button type="button" class="btn btn-success btn" id="increaseBreak"><span class="glyphicon glyphicon-menu-right"></span></div>
      </div>
      <div class="col-sm-6">
        <div class="btn-group">
          <button type="button" class="btn btn-success" id="decreaseWork"><span class="glyphicon glyphicon-menu-left"></span>
                <button type="button" class="btn btn-success"><span id="workTime">25:00</span>
              <button type="button" class="btn btn-success btn" id="increaseWork">     <span class="glyphicon glyphicon-menu-right"></span></div>
    </div>
    <br />
    <br />
    <br />
    <br />
    <div class="center">
      <div class="boxed">
        <br />
        <span id="boxText">SESSION</span>
        <br />
        <br />
        <br />
        <span id="display"></span>
        <br />
        <span id="timerStatus"></span>
      </div>
      <br />
      <button type="button" class="btn btn-success btn-sm" id="start">Start</button>
      <button type="button" class="btn btn-success btn-sm" id="pause">Pause</button>
      <button type="button" class="btn btn-success btn-sm" id="reset">Reset</button>
    </div>
  </div>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'>    </script>
  <script src='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js'></script>
  <script src="script.js"></script>
</body>
</html>

以及JavaScript:

var myInterval;
var workTime = document.getElementById("workTime").innerHTML = 25;
var breakTime = document.getElementById("breakTime").innerHTML = 5;
var remainSec = workTime * 60;
document.getElementById("display").innerHTML = workTime;
function startWork() {
  document.getElementById("start").disabled = true;
  timer(callback);
}
function pauseTime() {
  clearInterval(myInterval);
  document.getElementById("start").disabled = false;
}
function displayTime(remainingTime) {
  if (remainingTime % 60 >= 10) {
    document.getElementById('display').innerHTML = Math.floor(remainingTime / 60) + ':' + Math.floor(remainingTime % 60);
  } else {
    document.getElementById('display').innerHTML = Math.floor(remainingTime / 60) + ':' + "0" + Math.floor(remainingTime % 60);
  }
}
function timer(pomodoro) {
  var remainingTime = remainSec;
  myInterval = setTimeout(function() {
    displayTime(remainingTime);
    if (remainingTime >= 0) {
      remainSec--;
      timer(pomodoro);
    } else {
      clearInterval();
      pomodoro();
    }
  }, 1000);
}
var callback = function() {
  console.log('callback');
  document.getElementById('timerStatus').innerHTML = "Take a break!";
  remainSec = breakTime * 60;
  timer(callbackRest)
};
var callbackRest = function() {
  clearInterval(myInterval);
  console.log('callbackRest');
  document.getElementById('timerStatus').innerHTML = "Begin";
  remainSec = workTime * 60;
  document.getElementById("start").disabled = false;
};
function resetTime() {
  clearInterval(myInterval);
  remainSec = workTime * 60;
  startWork();
}
function decreaseWork() {
  if (workTime >= 1) {
    document.getElementById('workTime').innerHTML = --workTime;
    remainSec = workTime * 60;
  }
}
function decreaseBreak() {
  if (breakTime >= 1) {
    document.getElementById('breakTime').innerHTML = --breakTime;
  }
}
function increaseWork() {
  document.getElementById('workTime').innerHTML = ++workTime;
  remainSec = workTime * 60;
}
function increaseBreak() {
  document.getElementById('breakTime').innerHTML = ++breakTime;
}
document.getElementById('start').addEventListener('click', startWork);
document.getElementById('pause').addEventListener('click', pauseTime);
document.getElementById('reset').addEventListener('click', resetTime);
document.getElementById('decreaseWork').addEventListener('click', decreaseWork);
document.getElementById('decreaseBreak').addEventListener('click',  decreaseBreak);
document.getElementById('increaseWork').addEventListener('click', increaseWork);
document.getElementById('increaseBreak').addEventListener('click', increaseBreak);

非常感谢您的反馈!

我刚刚添加了一些内容。

添加了前两个标志,以了解isPomodoroTime状态和isBreakTime状态,也许您会问为什么是两个?因为我需要一个初始状态,即当两者都为false时,有了这些标志,我可以在不混淆的情况下增加和显示时间,因为有一个新函数被调用changeTimeAndDisplay,它正在从所有increasedecrease方法中调用。我还在回调中放了一个startWork,假设在休息时间结束时调用它。

这是一个演示-codepen.io

var myInterval;
var workTime = document.getElementById("workTime").innerHTML = 25;
var breakTime = document.getElementById("breakTime").innerHTML = 5;
var remainSec = workTime * 60;
var isPomodoroTime = false; //new
var isBreakTime = false; //new
//new function
function changeTimeAndDisplay(newTime){
  remainSec = newTime * 60;
  displayTime(remainSec);
}
document.getElementById("display").innerHTML = workTime;
function startWork() {
  isPomodoroTime = true; //new
  document.getElementById("start").disabled = true;
  timer(callback);
}
function pauseTime() {
  clearInterval(myInterval);
  document.getElementById("start").disabled = false;
}
function displayTime(remainingTime) {
  if (remainingTime % 60 >= 10) {
    document.getElementById('display').innerHTML = Math.floor(remainingTime / 60) + ':' + Math.floor(remainingTime % 60);
  } else {
    document.getElementById('display').innerHTML = Math.floor(remainingTime / 60) + ':' + "0" + Math.floor(remainingTime % 60);
  }
}
function timer(pomodoro) {
  var remainingTime = remainSec;
  myInterval = setTimeout(function() {
    displayTime(remainingTime);
    if (remainingTime >= 0) {
      remainSec--;
      timer(pomodoro);
    } else {
      clearInterval(myInterval); //new
      pomodoro();
    }
  }, 1000);
}
var callback = function() {
  isPomodoroTime = false; //new
  isBreakTime = true; //new
  console.log('callback');
  document.getElementById('timerStatus').innerHTML = "Take a break!";
  remainSec = breakTime * 60;
  timer(callbackRest)
};
var callbackRest = function() {
   isPomodoroTime = true; //new
  isBreakTime = false; //new 
  clearInterval(myInterval);
  console.log('callbackRest');
  document.getElementById('timerStatus').innerHTML = "Begin";
  remainSec = workTime * 60;
  document.getElementById("start").disabled = false;
  startWork(); //new
};
function resetTime() {
  clearInterval(myInterval);
  remainSec = workTime * 60;
  startWork();
}
function decreaseWork() {
  if (workTime >= 1) {
    document.getElementById('workTime').innerHTML = --workTime;
    if(isPomodoroTime || !isPomodoroTime && !isBreakTime){ //new if block
      changeTimeAndDisplay(workTime);
    }
  }
}
function decreaseBreak() {
  if (breakTime >= 1) {
    document.getElementById('breakTime').innerHTML = --breakTime;
    if(!isPomodoroTime && isBreakTime){ //new if block
      changeTimeAndDisplay(breakTime);
    }
  }
}
function increaseWork() {
  document.getElementById('workTime').innerHTML = ++workTime;
    if(isPomodoroTime || !isPomodoroTime && !isBreakTime){ //new if block
      changeTimeAndDisplay(workTime);
    }
}
function increaseBreak() {
  document.getElementById('breakTime').innerHTML = ++breakTime;
  if(!isPomodoroTime && isBreakTime){ //new if block
      changeTimeAndDisplay(breakTime);
    }
}
document.getElementById('start').addEventListener('click', startWork);
document.getElementById('pause').addEventListener('click', pauseTime);
document.getElementById('reset').addEventListener('click', resetTime);
document.getElementById('decreaseWork').addEventListener('click', decreaseWork);
document.getElementById('decreaseBreak').addEventListener('click',  decreaseBreak);
document.getElementById('increaseWork').addEventListener('click', increaseWork);
document.getElementById('increaseBreak').addEventListener('click', increaseBreak);

作为建议,您可以简化更多的代码,例如为document.getElementById(id).addEventListener(evnName,func); 创建一个函数

function addAction(evnName,id,selector){
   document.getElementById(id).addEventListener(evnName,func);
}

function addValue(id,value){
    document.getElementById(id).innerHTML = value;
}

并更换所有document.getElementById

问候。