I have the following code:
for (z=0;z<7;z++) {
x = Math.floor((Math.random() * 7));
//I am pulling some class names from an array here
var element = document.querySelector("." + map[x]);
//element.click();
console.log('No timeout ', x);
setTimeout(function() {
console.log('timeout ', x);
element.click();
}, 200);
}
Essentially, I am expecting to click a variety of random elements, however, when adding the click method in the setTimeOut function, I discovered that it clicked the same element. But when executed in the main code, it worked as expected, just a lot faster than I want!
To test this, I added some logs. The No timeout log produces numbers: 1,2,3,4,5,6,7 while the timeout log produces numbers: 7,7,7,7,7,7,7 (the last number from the No timeout log...
This explains why the same element is being clicked, I am not 100% sure why this is the case though?