我对此很困惑。我正在构建一个Pomodoro时钟,我已经使它运行得很好,直到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> Start </button>
<button class="btn btn-warning" onClick="reset()"><i class="fa fa-clock-o"></i> 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;
}
}
最佳答案
变量lesswork
和decreasetimer
声明为局部变量。因此,当调用函数downWork
时,每次lesswork
和decreasetimer
的值将为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;
}
}