此代码只需要按一次键盘即可播放音乐和动画一次。但是一次按下就可以播放两次动画。
这是我的代码。任何帮助表示赞赏。
var buttonColours=["red","yellow","blue","green"];
$(document).keydown(nextSequence);
function nextSequence(){
randomNumber=Math.floor(Math.random()*4);
randomChosenColour=buttonColours[randomNumber];
playSound(randomChosenColour);
animatePress(randomChosenColour);
}
function playSound(name){
var audio=new Audio(name+".mp3");
audio.play();
}
function animatePress(name){
$("#"+name).addClass("pressed");
{setTimeout(function(){$("#"+name).removeClass("pressed");},100)}
}