2017-09-24 14 views
-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))

它表明不确定,不知道我怎么能访问这个功能呢?

回答

0

想办法做到这一点,虽然不是很整齐。

$(".session").on("click", function() { 

//add function that it will stop when you click again 
//clearInterval(breakCounting);clearInterval(sessionCounting); 
pause = pause == true ? false : true; 


function runSession() { 
    var start = new Date().getTime()/1000; 
    var endTime = start + parseInt(sessionLength.html() * 60); 

    function sessionTimer() { 

    if (pause == true) { 
     clearInterval(sessionCounting); 
     return; 
    }; 
    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); 
     alertsound(); 
     runBreak(); 
    } 
    if (minutes < 0) { 
     minutes = 0; 
    } 
    if (seconds < 10) { 
     seconds = '0' + seconds; 
    } 
    $("#title").html("Session is Running!"); 
    $("#timer").html(minutes + ':' + seconds); 
    }; 

    var clearTimer = clearInterval(sessionCounting); 

    var sessionCounting = setInterval(sessionTimer, 1000); 
    sessionCounting; 

}; 



function runBreak() { 
    var start = new Date().getTime()/1000; 
    var endTime = start + parseInt(breakLength.html() * 60); 

    function breakTimer() { 

    if (pause == true) { 
     clearInterval(breakCounting); 
     return; 
    }; 

    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(); 
    }); 
}); 

代码片段我修改:

暂停=暂停==是真的吗?假:真;

我加入到sessionTimer()

代码片断:

if (pause == true) { 
    clearInterval(sessionCounting); 
    return; 
}; 

代码段,我加入到breakTimer():

if (pause == true) { 
    clearInterval(breakCounting); 
    return; 
}; 
相关问题