JavaScript Settimeout() Won't Wait to Execute

JavaScript setTimeout() won't wait to Execute?

alertBox()

Doesn't this look like an immediate function call?

Try passing the function (without executing it) instead:

setInterval(alertBox, 5000);

Why is the method executed immediately when I use setTimeout?

You're calling the function immediately and scheduling its return value.

Use:

setTimeout(testFunction, 2000);
^

Notice: no parens.

setTimeout() method won't execute

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.

setTimeout not calling callback to interrupt while loop

setTimeout will run the function you pass to it:

  • After the specified time has passed
  • After the minimum time for a timeout has passed
  • When the event loop isn't busy doing something else

… whichever is the latest.

You can't use a timeout to interrupt your while loop. The JavaScript engine is too busy running the while loop to check to see if there are any functions waiting to run on the timeout queue.

Your function will be called after the loop has finished when the JS engine is no longer busy.


Don't try to interrupt your loop from the outside.

Before you enter the while loop, record the current time.

Each time you go around the loop, compare the current time with the recorded time.

If the difference is greater than the period you want to allow the loop to run from, exit the loop

Waiting for timeout to finish before executing next line of code

I have a theory that this is because the pausing method I used only pauses the async function but not entirely sure if that is correct.

Indeed. You need to mark the mergeSort function as async as well, so you can await the merge() as well as the two recursive mergeSort() calls.

async function mergeSort(unsortedArray, aux = [...unsortedArray], lowIndex = 0, highIndex = unsortedArray.length - 1) { /*
^^^^^ */
if (highIndex === lowIndex) return;

const midIndex = Math.floor((highIndex + lowIndex) / 2);

await mergeSort(unsortedArray, aux, lowIndex, midIndex);
//^^^^^
await mergeSort(unsortedArray, aux, midIndex + 1, highIndex);
//^^^^^

await merge(unsortedArray, aux, lowIndex, midIndex, highIndex);
//^^^^^
}

setTimeout won't wait, even when waiting to call a function

setTimeout(slideIn(xslide), 20);

That invokes the slideIn function immediately. You aren't passing the slideIn function to setTimeout, you're passing the value returned by the slideIn function. You have to pass a function to setTimeout, which means you'd do something like this:

setTimeout(slideIn, 20);

However, this way, you don't get to pass in the parameter. So you wrap it in an anonymous function like this:

setTimeout(function (){
slideIn(xslide);
}, 20);

You could also give the function a name for debugging purposes like this:

setTimeout(function slideTimeout(){
slideIn(xslide);
}, 20);

Basically, if you run typeof on the first parameter of whatever you're passing to setTimeout or setInterval, it should return 'function'. What you're running will return 'undefined' since you're returning nothing.

Does the setTimeout() function wait until the function is executed before moving on to the next line?

It does not wait. The timer starts, then next line is executed, then sometime later setTimeout function fires



Related Topics



Leave a reply



Submit