如果多次单击 setInterval() 按钮,则不会触发 clearInterval()

我正在玩 Color Generator 应用程序,我添加了一个“迪斯科”功能,它会触发随机颜色“闪烁”到歌曲的节奏。顺便说一句,您将听不到它,但它是“为什么拒绝”:))


一切正常,但是:如果我多次单击“Disco”按钮,setInterval()将会加速(我不介意,事实上我喜欢它),但是一旦我决定通过滚动停止它,它就不会再被清除或在手机上滑动。


我在这里阅读了多个类似的问题,但没有一个有类似的问题,我真的不知道我能做什么。


如果多次单击,我想让它加速,但我也希望能够清除它。


let button = document.querySelector('.button')

let body = document.querySelector('.body')

let container = document.querySelector('.container')

let disco = document.querySelector('.disco')

let song = document.querySelector('.song')


button.addEventListener('click', ()=> {

  let colorOne = parseInt((Math.random() * 255) + 1)

  let colorTwo = parseInt((Math.random() * 255) + 1)

  let colorThree = parseInt((Math.random() * 255) + 1)


  body.style.background = 'rgb(' + colorOne + ', ' + colorTwo + ', ' + colorThree

  + ')'


  document.querySelector('.color').innerText = 'rgb (' + colorOne + ', ' + colorTwo + ', ' + colorThree

+ ')'


  button.style.border = 'none'

  document.querySelector('.scrollto').style.display = 'block'


  disco.style.display = 'none'

})


let dance = function() {

  let colorOne = parseInt((Math.random() * 255) + 1)

  let colorTwo = parseInt((Math.random() * 255) + 1)

  let colorThree = parseInt((Math.random() * 255) + 1)


  body.style.background = 'rgb(' + colorOne + ', ' + colorTwo + ', ' + colorThree

  + ')'

  }


let dancing;

let stopping;


disco.addEventListener('click', ()=> {

  document.querySelector('.scrollto').style.display = 'block'

  dancing = setInterval(dance,300)


  stopping = setTimeout(function() {

    clearInterval(dancing)

    button.style.display = 'block'

    body.style.background = 'white'

    document.querySelector('.scrollto').style.display = 'none'

   }, 15400)


  if(song.paused) {

    song.play()

    button.style.display = 'none'

  }

})


})


墨色风雨
浏览 110回答 1
1回答

潇湘沐

这是因为您在每次单击时都更改了变量的内容dancing。这意味着在单击 1 时它将引用 setInterval1,在单击 2 时将引用 setInterval2 等。然后当您尝试这样做时,clearInterval您实际上只清除了您创建的最后一个引用。您可以通过在添加新间隔之前简单地清除旧间隔来避免它:(我将停止事件更改为右键单击,例如目的)let button = document.querySelector('.button')let body = document.querySelector('.body')let container = document.querySelector('.container')let disco = document.querySelector('.disco')let song = document.querySelector('.song')button.addEventListener('click', ()=> {&nbsp; let colorOne = parseInt((Math.random() * 255) + 1)&nbsp; let colorTwo = parseInt((Math.random() * 255) + 1)&nbsp; let colorThree = parseInt((Math.random() * 255) + 1)&nbsp; body.style.background = 'rgb(' + colorOne + ', ' + colorTwo + ', ' + colorThree&nbsp; + ')'&nbsp; document.querySelector('.color').innerText = 'rgb (' + colorOne + ', ' + colorTwo + ', ' + colorThree+ ')'&nbsp; button.style.border = 'none'&nbsp; document.querySelector('.scrollto').style.display = 'block'&nbsp; disco.style.display = 'none'})let dance = function() {&nbsp; let colorOne = parseInt((Math.random() * 255) + 1)&nbsp; let colorTwo = parseInt((Math.random() * 255) + 1)&nbsp; let colorThree = parseInt((Math.random() * 255) + 1)&nbsp; body.style.background = 'rgb(' + colorOne + ', ' + colorTwo + ', ' + colorThree&nbsp; + ')'&nbsp; }let dancing;let stopping;disco.addEventListener('click', ()=> {&nbsp; document.querySelector('.scrollto').style.display = 'block'&nbsp; clearInterval(dancing);&nbsp; clearTimeout(stopping)&nbsp; dancing = setInterval(dance,300)&nbsp; stopping = setTimeout(function() {&nbsp; &nbsp; clearInterval(dancing)&nbsp; &nbsp; button.style.display = 'block'&nbsp; &nbsp; body.style.background = 'white'&nbsp; &nbsp; document.querySelector('.scrollto').style.display = 'none'&nbsp; &nbsp;}, 15400)&nbsp; if(song.paused) {&nbsp; &nbsp; //song.play()&nbsp; &nbsp; button.style.display = 'none'&nbsp; }})window.addEventListener('contextmenu', ()=> {&nbsp; body.style.background = 'white'&nbsp; document.querySelector('.color').innerText = ''&nbsp; document.querySelector('.scrollto').style.display = 'none'&nbsp; button.style.border = '1px solid black'&nbsp; clearInterval(dancing)&nbsp; clearTimeout(stopping)&nbsp; song.pause()&nbsp; song.currentTime = 0&nbsp; button.style.display = 'block'&nbsp; disco.style.display = 'block'}).button {&nbsp; font-family: 'Poppins', sans-serif;&nbsp; border-radius: .5em;&nbsp; padding: .3em .7em;&nbsp; font-size: 1.1em;&nbsp; position: relative;&nbsp; background: white;&nbsp; mix-blend-mode: screen;&nbsp; border: 1px solid black;}.color {&nbsp; font-family: 'Poppins', sans-serif;&nbsp; color: white;&nbsp; text-shadow: 1px 1px 3px black;&nbsp; letter-spacing: 1px;}.container {&nbsp; text-align: center;&nbsp; position: absolute;&nbsp; top: 40vh;&nbsp; left: 50vw;&nbsp; transform: translate(-50%, -50%);}.scrollto {&nbsp; position: absolute;&nbsp; bottom: 10px;&nbsp; left: 50vw;&nbsp; transform: translateX(-50%);&nbsp; font-family: 'Poppins', sans-serif;&nbsp; font-size: .7em;&nbsp; display: none;}.disco {&nbsp; position: absolute;&nbsp; bottom: 5px;&nbsp; right: 10px;&nbsp; font-family: 'Poppins', sans-serif;&nbsp; font-size: .8em;&nbsp; border: .5px solid black;&nbsp; border-radius: .3em;&nbsp; padding: 0 .3em;&nbsp; padding-top: .1em;}<body class="body">&nbsp;&nbsp;&nbsp; <div class="container">&nbsp; &nbsp; <h3 class="button">Generate Colour</h3>&nbsp; &nbsp; <p class="color"></p>&nbsp; </div>&nbsp; <div class="line">&nbsp; &nbsp; <p class="scrollto">swipe on screen to reset</p>&nbsp; </div>&nbsp; <h3 class="disco">Disco</h3>&nbsp; <audio class="song" src="song.mp3"></audio>编辑:从评论中,我看到你想保持加速效果:let button = document.querySelector('.button')let body = document.querySelector('.body')let container = document.querySelector('.container')let disco = document.querySelector('.disco')let song = document.querySelector('.song')button.addEventListener('click', ()=> {&nbsp; let colorOne = parseInt((Math.random() * 255) + 1)&nbsp; let colorTwo = parseInt((Math.random() * 255) + 1)&nbsp; let colorThree = parseInt((Math.random() * 255) + 1)&nbsp; body.style.background = 'rgb(' + colorOne + ', ' + colorTwo + ', ' + colorThree&nbsp; + ')'&nbsp; document.querySelector('.color').innerText = 'rgb (' + colorOne + ', ' + colorTwo + ', ' + colorThree+ ')'&nbsp; button.style.border = 'none'&nbsp; document.querySelector('.scrollto').style.display = 'block'&nbsp; disco.style.display = 'none'})let dance = function() {&nbsp; let colorOne = parseInt((Math.random() * 255) + 1)&nbsp; let colorTwo = parseInt((Math.random() * 255) + 1)&nbsp; let colorThree = parseInt((Math.random() * 255) + 1)&nbsp; body.style.background = 'rgb(' + colorOne + ', ' + colorTwo + ', ' + colorThree&nbsp; + ')'&nbsp; }let dancing;let stopping;let speed = 300;const accFactor = 1.5;disco.addEventListener('click', ()=> {&nbsp; document.querySelector('.scrollto').style.display = 'block'&nbsp; if(dancing) {&nbsp; &nbsp;clearInterval(dancing);&nbsp; &nbsp;clearTimeout(stopping);&nbsp; &nbsp; speed = speed/accFactor;&nbsp; }&nbsp; dancing = setInterval(dance,speed);&nbsp; stopping = setTimeout(function() {&nbsp; &nbsp; clearInterval(dancing)&nbsp; &nbsp; button.style.display = 'block'&nbsp; &nbsp; body.style.background = 'white'&nbsp; &nbsp; document.querySelector('.scrollto').style.display = 'none'&nbsp; &nbsp;}, 15400)&nbsp; if(song.paused) {&nbsp; &nbsp; //song.play()&nbsp; &nbsp; button.style.display = 'none'&nbsp; }})window.addEventListener('contextmenu', ()=> {&nbsp; body.style.background = 'white'&nbsp; document.querySelector('.color').innerText = ''&nbsp; document.querySelector('.scrollto').style.display = 'none'&nbsp; button.style.border = '1px solid black'&nbsp; clearInterval(dancing)&nbsp; clearTimeout(stopping)&nbsp; song.pause()&nbsp; song.currentTime = 0&nbsp; button.style.display = 'block'&nbsp; disco.style.display = 'block'}).button {&nbsp; font-family: 'Poppins', sans-serif;&nbsp; border-radius: .5em;&nbsp; padding: .3em .7em;&nbsp; font-size: 1.1em;&nbsp; position: relative;&nbsp; background: white;&nbsp; mix-blend-mode: screen;&nbsp; border: 1px solid black;}.color {&nbsp; font-family: 'Poppins', sans-serif;&nbsp; color: white;&nbsp; text-shadow: 1px 1px 3px black;&nbsp; letter-spacing: 1px;}.container {&nbsp; text-align: center;&nbsp; position: absolute;&nbsp; top: 40vh;&nbsp; left: 50vw;&nbsp; transform: translate(-50%, -50%);}.scrollto {&nbsp; position: absolute;&nbsp; bottom: 10px;&nbsp; left: 50vw;&nbsp; transform: translateX(-50%);&nbsp; font-family: 'Poppins', sans-serif;&nbsp; font-size: .7em;&nbsp; display: none;}.disco {&nbsp; position: absolute;&nbsp; bottom: 5px;&nbsp; right: 10px;&nbsp; font-family: 'Poppins', sans-serif;&nbsp; font-size: .8em;&nbsp; border: .5px solid black;&nbsp; border-radius: .3em;&nbsp; padding: 0 .3em;&nbsp; padding-top: .1em;}<body class="body">&nbsp;&nbsp;&nbsp; <div class="container">&nbsp; &nbsp; <h3 class="button">Generate Colour</h3>&nbsp; &nbsp; <p class="color"></p>&nbsp; </div>&nbsp; <div class="line">&nbsp; &nbsp; <p class="scrollto">swipe on screen to reset</p>&nbsp; </div>&nbsp; <h3 class="disco">Disco</h3>&nbsp; <audio class="song" src="song.mp3"></audio>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript