-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))
它表明不確定,不知道我怎麼能訪問這個功能呢?