Settimeout() Method Won't Execute
I have written a script, but i want to limit execution time for some functions. I decided to try setTimeout() method, however it results in no time out when i execute the program,
Solution 1:
The code should look like this:
setTimeout(rollDice, 6000);
By adding the parentheses, you're calling the function and setting a timer for calling whatever that function returns. You'll want to pass the function object itself.
You can't immediately use diceOne
and diceTwo
after setting the timeout. You have to put that code in the timeout function as well, for example:
setTimeout(function() {
rollDice();
if (diceOne != 1 && diceTwo != 1){
currentScore += diceOne + diceTwo;
...
}, 6000);
That's because the code after setTimeout
will not wait before the timeout has finished.
Solution 2:
Try this:
setTimeout(function(){
rollDice()
}, 6000)
Solution 3:
There is no need of Brackets when calling rollDice Function.
setTimeout(rollDice, 6000);
Solution 4:
I would use something like this:
setTimeout(function(){rollDice()}, 6000);
Post a Comment for "Settimeout() Method Won't Execute"