-1
这是我的代码笔链接:建立一个番茄时间时钟项目,需要访问调用clearInterval功能,但卡在封装
https://codepen.io/thinkerElwin/pen/gGMvwR?editors=1010
我的代码片段在这里:
$(".session").on("click", function() {
pause ? runSession() : (clearInterval(sessionCounting));
function runSession() {
pause = false;
var start = new Date().getTime()/1000;
var endTime = start + parseInt(sessionLength.html() * 60);
function sessionTimer() {
time = new Date().getTime()/1000;
seconds = Math.ceil((endTime - time) % 60); // ceil for less than starting time
minutes = Math.floor((endTime - time)/60);
console.log(Math.ceil(endTime - time));
if (minutes <= 0 && seconds == 0) {
clearInterval(sessionCounting);
alert();
runBreak();
}
if (minutes < 0) {
minutes = 0;
}
if (seconds < 10) {
seconds = '0' + seconds;
}
$("#title").html("Session is Running!");
$("#timer").html(minutes + ':' + seconds);
};
var sessionCounting = setInterval(sessionTimer, 1000);
sessionCounting;
};
function runBreak() {
var start = new Date().getTime()/1000;
var endTime = start + parseInt(breakLength.html() * 60);
function breakTimer() {
time = (new Date().getTime())/1000;
seconds = Math.ceil((endTime - time) % 60); // ceil for less than starting time
minutes = Math.floor((endTime - time)/60);
console.log(Math.ceil(endTime - time));
if (minutes <= 0 && seconds == 0) {
clearInterval(breakCounting);
alertsound();
runSession();
}
if (minutes < 0) {
minutes = 0;
}
if (seconds < 10) {
seconds = '0' + seconds;
}
$("#title").html("Time to take a break~");
$("#timer").html(minutes + ':' + seconds);
};
var breakCounting = setInterval(breakTimer, 1000);
breakCounting;
};
});
});
暂停=真正的开始
我想添加一个函数来停止计时器滴答时用户再次点击运行计时器,现在我用这个:
暂停? runSession():(clearInterval(sessionCounting),clearInterval(breakCounting));
但它失败了,当我使用
consol.log(clearInterval(sessionCounting))
它表明不确定,不知道我怎么能访问这个功能呢?