I am trying to color some rectangles, but want it done with one second intervals so I can see them changing color one by one.
I have tried using setTimeout(); and have looked at many different answers in stack overflow but I have not been able to get it to work, currently when I use this code the time delay seems to add up and then all the blocks change color at the same time.
function dijkstra(){
for (y = 0; y < 10; y++){
for(x = 0; x < 10; x++){
const div = document.getElementById(`${x} ${y}`);
setTimeout(function() {
div.setAttribute('class', 'searched')
}, 500);
}
}
}
I am using the code below to generate the code
for (y = 0; y < 10; y++){
const row = document.createElement('div');
row.setAttribute('class', y);
for(x = 0; x < 10; x++){
const block = document.createElement('div');
block.setAttribute('class', 'unsearched');
block.setAttribute('id', `${x} ${y}`);
row.appendChild(block);
}
body.appendChild(row);
}
and this is what my html looks like
<!DOCTYPE html>
<head>
<title>Path Finding</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id = "navbar">
<ul id="ulist">
<li id="start" onclick="dijkstra()"><a>start</a></li>
</ul>
</div>
<div class="big_div">
<div class="big_container">
</div>
</div>
<script type="text/javascript" src="script.js"></script>
</body>