2021年1月1日星期五

Switch case for event listeners sometimes retriggers event

I'm using a switch statement to handle keydown events for a music player app. Sometimes when you press spacebar, it results in the event retriggering (you can see what I mean in the live version) - the song pauses, the intended result, but instead of staying paused, it pauses for an instant then plays again. Why does this happen?

document.addEventListener('keydown', (e) => {    switch (e.code) {      case 'ArrowLeft':        prevSong();        break;      case 'ArrowRight':        nextSong();        break;      case 'Space':        const isPlaying = musicContainer.classList.contains('play');        isPlaying ? pauseSong() : playSong();        break;    }  });  
https://stackoverflow.com/questions/65535566/switch-case-for-event-listeners-sometimes-retriggers-event January 02, 2021 at 10:34AM

没有评论:

发表评论