I have some JS functions that are supposed to, essentially, display a number on a webpage every 5 seconds. In reality, what happens is that one number is displayed on screen, but it is not updated after 5 seconds (or ever, actually); it simply stays there forever. Curiously, if I add debug points (via Chrome developer tools), the loop works and the number on the screen is updated as expected.
There just seems to be an issue with the loop executing as it should in regular mode (i.e. not in debug mode).
For reference, I have tested in both Safari and Chrome and the issue is the same.
function displayNumbers(){
document.getElementById("instructions").innerHTML = "";
document.getElementById("welcome-message").innerHTML = "";
var numbersCalled = [];
for (var j=0; j< 90; j++){
var numberToDisplay = Math.floor(Math.random() * 91) + 1;
if (!numbersCalled.includes(numberToDisplay)){
setTimeout(printNumberToScreen(numberToDisplay), 5000);
numbersCalled.push(numberToDisplay);
}
}
}
function printNumberToScreen(numberToDisplay){
document.getElementById("number-display").innerHTML = numberToDisplay;
}
'number-display' is initialised as:
<h2 id="number-display"></h2>