I'm making a simple dice game, and I have set up a function that will randomly generate numbers and display them very quickly to simulate a sort of die being rolled. The problem I'm having is that I want to wait until the simulation is complete before I return the value, but instead the value is returned right away. Can anyone point me in the right direction? Essentially I want rollDie() to wait for diceSimulation() to be complete before continuing.
function rollDie(){
diceSimulation();
var result = Math.floor(Math.random() * 6) + 1;
document.getElementById("result").innerHTML = result;
return result;
}
function diceSimulation(){
for (var i = 0; i < 30; i++) {
var random;
(function (i) {
setTimeout(function () {
random = Math.floor(Math.random() * 6) + 1;
document.getElementById("result").innerHTML = random;
}, 50*i);
})(i);
}
}