第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如果多次單擊 setInterval() 按鈕,則不會(huì)觸發(fā) clearInterval()

如果多次單擊 setInterval() 按鈕,則不會(huì)觸發(fā) clearInterval()

墨色風(fēng)雨 2023-06-15 09:47:57
我正在玩 Color Generator 應(yīng)用程序,我添加了一個(gè)“迪斯科”功能,它會(huì)觸發(fā)隨機(jī)顏色“閃爍”到歌曲的節(jié)奏。順便說一句,您將聽不到它,但它是“為什么拒絕”:))一切正常,但是:如果我多次單擊“Disco”按鈕,setInterval()將會(huì)加速(我不介意,事實(shí)上我喜歡它),但是一旦我決定通過滾動(dòng)停止它,它就不會(huì)再被清除或在手機(jī)上滑動(dòng)。我在這里閱讀了多個(gè)類似的問題,但沒有一個(gè)有類似的問題,我真的不知道我能做什么。如果多次單擊,我想讓它加速,但我也希望能夠清除它。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'  }})})
查看完整描述

1 回答

?
瀟湘沐

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊

這是因?yàn)槟诿看螁螕魰r(shí)都更改了變量的內(nèi)容dancing。這意味著在單擊 1 時(shí)它將引用 setInterval1,在單擊 2 時(shí)將引用 setInterval2 等。然后當(dāng)您嘗試這樣做時(shí),clearInterval您實(shí)際上只清除了您創(chuàng)建的最后一個(gè)引用。


您可以通過在添加新間隔之前簡(jiǎn)單地清除舊間隔來避免它:


(我將停止事件更改為右鍵單擊,例如目的)


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'

  clearInterval(dancing);

  clearTimeout(stopping)

  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'

  }

})



window.addEventListener('contextmenu', ()=> {

  body.style.background = 'white'

  document.querySelector('.color').innerText = ''

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

  button.style.border = '1px solid black'


  clearInterval(dancing)

  clearTimeout(stopping)

  song.pause()

  song.currentTime = 0

  button.style.display = 'block'

  disco.style.display = 'block'

})

.button {

  font-family: 'Poppins', sans-serif;

  border-radius: .5em;

  padding: .3em .7em;

  font-size: 1.1em;

  position: relative;

  background: white;

  mix-blend-mode: screen;

  border: 1px solid black;

}


.color {

  font-family: 'Poppins', sans-serif;

  color: white;

  text-shadow: 1px 1px 3px black;

  letter-spacing: 1px;

}


.container {

  text-align: center;

  position: absolute;

  top: 40vh;

  left: 50vw;

  transform: translate(-50%, -50%);

}


.scrollto {

  position: absolute;

  bottom: 10px;

  left: 50vw;

  transform: translateX(-50%);

  font-family: 'Poppins', sans-serif;

  font-size: .7em;

  display: none;

}


.disco {

  position: absolute;

  bottom: 5px;

  right: 10px;

  font-family: 'Poppins', sans-serif;

  font-size: .8em;

  border: .5px solid black;

  border-radius: .3em;

  padding: 0 .3em;

  padding-top: .1em;

}

<body class="body">

  

  <div class="container">

    <h3 class="button">Generate Colour</h3>

    <p class="color"></p>

  </div>


  <div class="line">

    <p class="scrollto">swipe on screen to reset</p>

  </div>


  <h3 class="disco">Disco</h3>

  <audio class="song" src="song.mp3"></audio>

編輯:


從評(píng)論中,我看到你想保持加速效果:


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;

let speed = 300;

const accFactor = 1.5;


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

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

  if(dancing) {

   clearInterval(dancing);

   clearTimeout(stopping);

    speed = speed/accFactor;

  }

  dancing = setInterval(dance,speed);


  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'

  }

})



window.addEventListener('contextmenu', ()=> {

  body.style.background = 'white'

  document.querySelector('.color').innerText = ''

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

  button.style.border = '1px solid black'


  clearInterval(dancing)

  clearTimeout(stopping)

  song.pause()

  song.currentTime = 0

  button.style.display = 'block'

  disco.style.display = 'block'

})

.button {

  font-family: 'Poppins', sans-serif;

  border-radius: .5em;

  padding: .3em .7em;

  font-size: 1.1em;

  position: relative;

  background: white;

  mix-blend-mode: screen;

  border: 1px solid black;

}


.color {

  font-family: 'Poppins', sans-serif;

  color: white;

  text-shadow: 1px 1px 3px black;

  letter-spacing: 1px;

}


.container {

  text-align: center;

  position: absolute;

  top: 40vh;

  left: 50vw;

  transform: translate(-50%, -50%);

}


.scrollto {

  position: absolute;

  bottom: 10px;

  left: 50vw;

  transform: translateX(-50%);

  font-family: 'Poppins', sans-serif;

  font-size: .7em;

  display: none;

}


.disco {

  position: absolute;

  bottom: 5px;

  right: 10px;

  font-family: 'Poppins', sans-serif;

  font-size: .8em;

  border: .5px solid black;

  border-radius: .3em;

  padding: 0 .3em;

  padding-top: .1em;

}

<body class="body">

  

  <div class="container">

    <h3 class="button">Generate Colour</h3>

    <p class="color"></p>

  </div>


  <div class="line">

    <p class="scrollto">swipe on screen to reset</p>

  </div>


  <h3 class="disco">Disco</h3>

  <audio class="song" src="song.mp3"></audio>


查看完整回答
反對(duì) 回復(fù) 2023-06-15
  • 1 回答
  • 0 關(guān)注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)