递减按钮以停止在1

时间:2016-01-24 04:27:51

标签: javascript html function if-statement decrement

我对此感到非常困惑。我正在构建一个番茄钟,我已经让它运行得很好,直到1,然后停在调整器显示器和时钟本身。我添加了几行代码,它不再停在一个,它变为零,-1等。即使我删除了新行并测试它不再有效。我知道这应该相当容易,但我似乎无法让它正常工作并保持工作。任何帮助表示赞赏。

 <div id="center">
    <p id="title">Pomodoro Clock</p>
    <p id="mytimer">25</p>

    <div class="buttons">
    <button class="btn btn-primary" onClick="start()"><i class="fa fa-clock-o"></i>&nbsp;&nbsp; Start &nbsp;       </button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <button class="btn btn-warning" onClick="reset()"><i class="fa fa-clock-o"></i>&nbsp; Reset</button>
  </div>
 </div>

  <div id="workArrows">
      <h6>Session Time</h6>
      <button class="btn btn-info arrowbtn" onClick="upWork()"> <i class="fa fa-arrow-up"></i></button>
       <h4 class="arrownum" id="sessiontime">25</h4>
       <button class="btn btn-info arrowbtn" onClick="downWork()"><i class="fa fa-arrow-down"></i></button> 
  </div>

function downWork(){
  var lesswork = 25; //decrease time adjuster
  var decreasetimer = 25; //decrease the clock respectively

  if(lesswork > 1){
  lesswork = document.getElementById("sessiontime").innerHTML;
  lesswork--;
  document.getElementById("sessiontime").innerHTML= lesswork;

  decreasetimer = document.getElementById("mytimer").innerHTML;
  decreasetimer--;
  document.getElementById("mytimer").innerHTML = decreasetimer;

}else{
  lesswork = 1;
  document.getElementById("sessiontime").innerHTML = lesswork;

  decreasetimer = 1;
  document.getElementById("mytimer").innerHTML = decreasetimer;
    }
}

2 个答案:

答案 0 :(得分:1)

在检查值之前,需要将lesswork变量设置为等于HTML元素的值。现在你在检查它是否大于1之前将它设置为25,然后将其设置为等于HTML元素。

答案 1 :(得分:1)

变量lessworkdecreasetimer声明为局部变量。因此,当调用函数downWork时,lessworkdecreasetimer的值每次都为25。

您需要将变量声明为全局。

var lesswork = 25; //decrease time adjuster
var decreasetimer = 25; //decrease the clock respectively

function downWork(){
  console.log(lesswork);
  if(lesswork > 1){
    lesswork = document.getElementById("sessiontime").innerHTML;
    lesswork--;
    document.getElementById("sessiontime").innerHTML= lesswork;

    decreasetimer = document.getElementById("mytimer").innerHTML;
    decreasetimer--;
    document.getElementById("mytimer").innerHTML = decreasetimer;

}else{
        lesswork = 1;
        document.getElementById("sessiontime").innerHTML = lesswork;

          decreasetimer = 1;
          document.getElementById("mytimer").innerHTML = decreasetimer;
    }
}