Я получаю (что кажется) случайную ошибку обещания во время моего оператора switch нижеЯ создал случайное число и связал его с заданным положением элементов массива.Я использую эту позицию внутри своего оператора switch для воспроизведения определенного звука.При тестировании этого в браузере я иногда получаю исключение в обещании, как показано ниже:
playSound @ game.js:29
nextSequence @ game.js:18
(anonymous) @ game.js:45
Несмотря на то, что я получаю эту ошибку, все работает, как задумано.РЕДАКТИРОВАТЬ ЧАСТЬ: при отладке с помощью отладчика;вышеупомянутое присутствует с перерывами
//store colors
var buttonColors = [
"green", //0
"red", //1
"yellow", //2
"blue" //3
]
gamePattern = [ /*Added From nextSequence*/ ]
//Generate a random number
function nextSequence() {
randomNumber = Math.floor(Math.random() * 4)
randomChosenColor = buttonColors[randomNumber];
gamePattern.push(randomChosenColor);
$(`#` + randomChosenColor).fadeOut(100).fadeIn(100).delay(200);
playSound(randomChosenColor);
}
function playSound(color) {
switch (color) {
case 'green':
var greenButton = new Audio('sounds/green.mp3');
greenButton.play();
break;
case 'red':
var redButton = new Audio(`sounds/red.mp3`);
redButton.play();
break;
case `yellow`:
var yellowButton = new Audio(`sounds/yellow.mp3`);
yellowButton.play();
break;
case `blue`:
var blueButton = new Audio(`sounds/blue.mp3`);
blueButton.play();
break;
default:
console.log(`Play Sound Error in playSound Function`)
}
}
nextSequence();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>