Хорошо, давайте сначала немного почистим. Вы создаете цикл для создания ячеек, затем повторяете цикл для их изменения, вы должны просто изменить их сразу.
if (userchioce == 4) {
var table = document.getElementById("mytable");
document.getElementById("mytable").innerHTML = "";
var idnum = 0;
for (var i = 0; i < 2; i++) {
var row = table.insertRow(0);
for (var j = 0; j < 2; j++) {
var cell = row.insertCell(-1);
cell.style.backgroundColor = colorarry[idnum];
cell.innerHTML = colorarry1[idnum];
cell.setAttribute("class", "td1");
cell.setAttribute("id", "tdd" + idnum++);
}
}
}
Я также удалил переменную counter
в пользу idnum
переменная. Они оба были определены в 0 в одном и том же месте, а также увеличивались с одинаковой скоростью ...
Вы не можете отображать огни один за другим, потому что вы делаете это только один раз. Там должно быть место, где вы будете отслеживать предыдущие рандомы.
var moves = [];
function newTurn() {
var rnd = Math.floor(Math.random() * userchioce);
// Add the new random to the moves history.
moves.push(rnd);
//create a copy, we'll be playing with it.
var movesToShow = moves.slice();
showMove();
}
function showMove(moveList){
//Remove first value of the list of moves and use it to show.
var move = moveList.shift();
var id = "tdd";
var fullid = id + move;
var display= document.getElementById(fullid);
display.classList.add("flash");
//Wait a little before removing the hightlight.
setTimeout(function () {
display.classList.remove("flash");
if(moveList.length>0){
//There are more moves, wait just a little
setTimeout(function(){
//Display a new move.
showMove(moveList);
},100);
}
}, 850);
}
// call this to start a new turn.
newTurn();
Также я хотел бы призвать вас исправить все опечатки в вашем скрипте. «dispaly», «userchioce», это усложнит вам задачу.