我有2个功能,我希望第一个按键运行一个功能时要比另一个按键运行第二个功能要好!
是spaceBar的播放/暂停视频功能!
git push
答案 0 :(得分:1)
所有有关保存布尔值的信息,让您知道是否所有视频都在播放,然后在if语句中使用它。
let playing = false;
const playAllVideos = () => console.log('playing all videos');
const pauseAllVideos = () => console.log('paused all videos');
const playPause = e => {
let key = e.keyCode || e.which;//Get key
if(key == 32) { //Space key
if(playing) {//If playing turn all videos off
pauseAllVideos();
playing = false;
} else {//vise versa
playAllVideos();
playing = true;
}
}
}
window.addEventListener('keypress', playPause);
<p>Press Space</p>
答案 1 :(得分:0)
您需要将当前状态(即播放或暂停)存储在变量中:
let paused = false
function onKeyPressed(e) {
if (e.keyCode == 23) {
if (paused) playAllVideos()
else pauseAllVideos()
}
}