JavaScript - 如何切换 setInterval

JavaScript - how to toggle setInterval

在我的 Vue 应用程序中,我试图在两个间隔之间切换,这取决于我想要显示 "This is first interval" 或 "This is second interval" 的切换器标志(真或假)。我的方法:

data() {
   return {
     switcher: false
   }
},

methods: {
 switchInterval() {
   this.switcher = !this.switcher;
   if(this.switcher) {
       let firstInterval = setInterval(() => {
         console.log('This is first interval')
       }, 1000)
    if(this.switcher === false) clearInterval(firstInterval);
     }

      if(!this.switcher) {
         let secondInterval = setInterval(() =>{
             console.log('This is second interval')
          }, 1000);

      if(this.chatSwitch === true) clearInterval(secondInterval);
 }
}

有人可以帮我解决这个问题吗?

toggleInterval() {
      let handler;
      this.switcher = !this.switcher;

      if (this.switcher) {
        handler = () => {
          console.log('This is first interval');
        };
      } else {
        handler = () => {
          console.log('This is first interval');
        };
      }
      clearInterval(this.intervalId);
      this.intervalId = setInterval(handler, 1000);
},

info on setInterval