0

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, i.e. setTimeout() does not work.

setTimeout(rollDice(), 6000) is the line that executes instantly

Here is my code :

function rollDice() {
diceOne = Math.round(5 * Math.random() + 1);
diceTwo = Math.round(5 * Math.random() + 1);
}
function mainFunction() {
playerAI.playing = true;
playerOne.playing = true;
currentScore = 0;
playerAI.totalScore = 0;
playerOne.totalScore = 0;
while (playerAI.playing == true && playerOne.playing == true) {
    makeMove();
}
}
function makeMove() {
if (who == 0) {
    aiStrat();
    game();
}
else {
    var confirmAction = confirm("Kas soovite visata täringuid?");
    if (confirmAction) {
    decision = 1;
    }
    else {
    decision = -1;
    }
    game();
}
}
function game() {
if (decision == 1) {
    setTimeout(rollDice(), 6000); // <--- THIS GETS EXECUTED INSTANTLY
    if (diceOne != 1 && diceTwo != 1){
        currentScore += diceOne + diceTwo;
//and so on
Evald
  • 122
  • 10

4 Answers4

4

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.

Overv
  • 8,433
  • 2
  • 40
  • 70
  • 1
    if you wanted to pass parameters to rollDice, you could use setTimeout(function(){ rollDice(1); }, 6000); as well – 0x6A75616E Oct 05 '14 at 21:05
  • it does not wait 6 seconds and gets executed immideately – Evald Oct 05 '14 at 21:07
  • @Evald The code you have after `setTimeout` should also be called within the timeout function. I'll update my answer. – Overv Oct 05 '14 at 21:10
  • I'm sorry, it actually does work the correct way, it was my mistake. diceOne and diceTwo do get new value after 6 seconds, i was mistaken by the confirm() function, that comes later and pops up immideately. I thought setTimeout() would stop the entire loop for 6 seconds at given line, but it goes on despite the timeOut for one function – Evald Oct 05 '14 at 21:17
  • @Evald Correct, when you call `setTimeout` or `setInterval`, all you're doing is scheduling some code to be run at a later time. – Overv Oct 05 '14 at 21:19
0

Try this:

setTimeout(function(){
    rollDice()
}, 6000) 
MaximOrlovsky
  • 265
  • 2
  • 8
0

There is no need of Brackets when calling rollDice Function.

    setTimeout(rollDice, 6000);
Syed Daniyal Asif
  • 726
  • 1
  • 5
  • 19
0

I would use something like this:

setTimeout(function(){rollDice()}, 6000);

user123_456
  • 5,635
  • 26
  • 84
  • 140