How do I clear this setInterval inside a function?

前端 未结 4 1420
眼角桃花
眼角桃花 2020-11-29 21:26

Normally, I’d set the interval to a variable and then clear it like var the_int = setInterval(); clearInterval(the_int); but for my code to work I put it in an

4条回答
  •  既然无缘
    2020-11-29 21:50

    // Initiate set interval and assign it to intervalListener
    var intervalListener = self.setInterval(function () {someProcess()}, 1000);
    function someProcess() {
      console.log('someProcess() has been called');
      // If some condition is true clear the interval
      if (stopIntervalIsTrue) {
        window.clearInterval(intervalListener);
      }
    }
    

提交回复
热议问题