let innerArrayOne = [1,"red","fed"] let innerArrayTwo = [2,"blue","you"] let innerArrayThree = [3,"green","bean"] let innerArrayFour = [4,"yellow","fellow"] let arrayS = [innerArrayOne,innerArrayTwo,innerArrayThree, innerArrayFour]Esto es lo que he intentado hasta ahora
for(let i = 0; i < arrayOfDoom.length; i++){ console.log("----") console.log(arrayOfDoom[i]) console.log("----") for(let j = 0; j < arrayOfDoom[i].length;j++) console.log(arrayOfDoom[i][j]) } }Aquí están los datos de prueba que he hecho. Esto es lo que he intentado hasta ahora. Eso te da cada elemento en la matriz.
function loop(count, callback, done) { let counterForLoop = 0; let next = function () {setTimeout(iteration, 500);}; let iteration = function () { if (counterForLoop < count) { callback(counterForLoop, next); } else { done && done();} counterForLoop++; } iteration(); } loop(10000, function (i, next) {next();}) loop(9, function (i, nextI) { console.log("----") console.log(i) console.log("----") loop(3, function (j, nextJ) { console.log(arrayS[i][j]);nextJ(); }, nextI);});Esto es del único otro StackOverflow cercano que verifiqué. Funciona, pero termina después del elemento final en la primera matriz. No entiendo completamente la recursividad, así que cada vez que he intentado editar, como poner el contador a cero después de llegar a 8, se rompe. Me gustaría que la salida fuera
1 red fed 2 blue you ....Con un elemento saliendo, cada segundo durante uno o dos minutos, y el ciclo una vez que llega al final, solo necesita repetirse.
¿Alguna idea o qué debería investigar?
Los futuros homies también deberían considerar dividir el problema en dos: (1) hacer cualquier cosa en un intervalo durante un tiempo fijo y (2) iniciar sesión al azar.
Un pequeño disfraz de setInterval puede proporcionar puede ocultar el tiempo matemático y detenerse después de un tiempo transcurrido especificado.
El registro aleatorio también se puede mejorar mezclando y luego registrando secuencialmente, para obtener aleatoriedad sin repetición.
let innerArrayOne = [1,"red","fed"]; let innerArrayTwo = [2,"blue","you"]; let innerArrayThree = [3,"green","bean"]; let innerArrayFour = [4,"yellow","fellow"]; let arrayS = [innerArrayOne,innerArrayTwo,innerArrayThree, innerArrayFour]; // fisher-yates shuffle, thanks to https://stackoverflow.com/a/2450976/294949 function shuffle(array) { let currentIndex = array.length, randomIndex; while (currentIndex != 0) { randomIndex = Math.floor(Math.random() * currentIndex); currentIndex--; [array[currentIndex], array[randomIndex]] = [ array[randomIndex], array[currentIndex]]; } return array; } // invoke fn on a set interval until elapsed ms have elapsed function repeat(fn, interval, elapsed) { fn() // up to author if we start right away const start = (new Date()).getTime(); // ms since epoch const id = setInterval(() => { const now = (new Date()).getTime(); if (now - start > elapsed) { clearInterval(id); } else { fn() } }, interval); } let shuffledArray = shuffle(arrayS.slice()); let index = 0; repeat(() => { console.log(shuffledArray[index]) if (index < shuffledArray.length-1) index++; else { shuffledArray = shuffle(arrayS.slice()); index = 0; } }, 800, 4800)Para todos los futuros homies, así es como lo hice funcionar
function getRandomInt(max) { return Math.floor(Math.random() * max); } let i = 0; let loopCounter = 0; let randomNumber = 1 const outerLoop = () => { for (i; i < arrayS.length;){ console.log(randomNumber); console.log(arrayS[randomNumber][loopCounter]) i++; loopCounter++; if(loopCounter === 3){ loopCounter = 0 randomNumber = getRandomInt(8) } if(i === 4){ i = 0 } break; } } setInterval(outerLoop, 2000);