Javascript 构建一个Pomotoro时钟项目,需要访问clearinterval函数,但仍停留在封装上

Javascript 构建一个Pomotoro时钟项目,需要访问clearinterval函数,但仍停留在封装上,javascript,Javascript,这是我的代码笔链接: 我的代码片段如下: $(".session").on("click", function() { pause ? runSession() : (clearInterval(sessionCounting)); function runSession() { pause = false; var start = new Date().getTime() / 1000; var endTime = start + parseInt(ses

这是我的代码笔链接:

我的代码片段如下:

$(".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;
  };
  });
});
$(.session”)。在(“单击”,函数(){
暂停?runSession():(clearInterval(sessionCounting));
函数runSession(){
暂停=错误;
var start=new Date().getTime()/1000;
var endTime=start+parseInt(sessionLength.html()*60);
函数sessionTimer(){
时间=新日期().getTime()/1000;
秒=Math.ceil((endTime-time)%60);//小于开始时间的ceil
分钟=数学楼层((结束时间-时间)/60);
log(Math.ceil(endTime-time));

如果(分钟找到这样做的方法,尽管不是很整洁

$(".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();
  });
});
我添加到breakTimer()的代码段:

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