Зацикливание на массиве звуковых элементов - PullRequest
0 голосов
/ 10 декабря 2018

Я пытаюсь выполнить цикл, который проходит через массив звуков, и идея состоит в том, чтобы воспроизводить звуки один за другим.Я попытался создать forEach и попытался установить setTimeOut в цикле for и forEach, но у меня это не сработало.Любая идея, как я могу это сделать?

Итак, идея состоит в том, чтобы просмотреть массив «результат» и воспроизводить звуки, которые он содержит один за другим (прямо сейчас, когда я использую цикл for / forEachя получаю все звуки одновременно.)

Мой код:

// getting the files - buttons
const buttonYel = document.getElementById("btnYellow")
const buttonGre = document.getElementById("btnGreen")
const buttonBlue = document.getElementById("btnBlue")
const buttonRed = document.getElementById("btnRed")
const button = document.querySelector("button")
// getting the files - sounds
const soundRed = document.getElementById("soundRed")
const soundYel = document.getElementById("soundYellow")
const soundBlue = document.getElementById("soundBlue")
const soundGre = document.getElementById("soundGreen")

let result = [];
let userResult = [];
let currectPickComp;
let currectPickUser;

button.addEventListener("click", gameStart)
function gameStart() {


setTimeout(()=> {
let random = Math.floor(Math.random()* 4) + 1;
if (random === 1){
    result.push(soundYel)
    soundYel.play();
    buttonYel.style.animation = "shake 0.5s";
    currectPickComp = soundYel;
} else if (random === 2){
    result.push(soundRed);
    soundRed.play();  
    buttonRed.style.animation = "shake 0.5s"; 
    currectPickComp = soundRed;
} else if (random === 3){
    result.push(soundBlue)
    soundBlue.play();
    buttonBlue.style.animation = "shake 0.5s";  
    currectPickComp = soundBlue; 
} else if (random === 4){
    result.push(soundGre);
    soundGre.play()   
    buttonGre.style.animation = "shake 0.5s";
    currectPickComp = soundGre;  
}}, 500)}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...