clearInterval 不适用于随机播放功能

clearInterval not working on shuffle function

我创建的切换按钮无法使用我的 clearInterval 函数。

$('button.shuffle').click(function() {
  if(isShuffling) {
    clearInterval(discoLights);
    isShuffling = false;
    console.log('stop shuffling')
  } else {
    disco = setInterval(discoLights ,3000);
    isShuffling = true;
    console.log('start shuffling')
  }
});

我知道这种问题已经被问过很多次了。我在 SO 中对此进行了大量研究,但找不到解决问题的方法。

这是我一直在处理的代码的 JSFiddle:https://jsfiddle.net/coolwebs/zLgsdno7/7/

我曾多次尝试重新排序代码,但没有任何效果。我在页面加载之前使用过 setInterval 和 clearInterval(未使用按钮设置)并且我之前没有遇到过问题...

使用

clearInterval(disco);

而不是

clearInterval(discoLights);

作为clearInterval takes the id which is returned by setInterval

问题在 clearInterval()

应该是clearInterval(disco)而不是

clearInterval(dicoLights).

clearInterval