Jump to content

How do I delay code from executing?


kody
 Share

Recommended Posts

I have a function that calls another function inside a "for" loop.

The second function uses setInterval() to display and update a countdown clock on the web page.

The user sets the time on the countdown clock in minutes, and also sets the number of times the loop will operate (how many countdown clocks).

I want the code in the first function to wait until the clock reaches zero before continuing.

 

function firstFunction(){

for(I=1; I<count; I++){

some code...

secondFunction();

more code...

}

 

My one idea was to create a global variable/flag with a "while" loop with nothing inside the brackets just after calling the second function.

In the second function, I would set the flat to "Y" when the timer reaches zero.

But, the web page just locks up.

 

while (flag=="N"){ };

 

Help would be appreciated.

Edited by kody
Link to comment
Share on other sites

They way to do this is to have global variables and use setInterval. Here's an example:

var i = 0;
var count = 10;

function loop() {
  if(i < count) {
    // Do something
    i++;
  }
}

setInterval(loop, 500);

In Javascript, the browser stops until a while loop has ended, so you can't use a while loop for timing.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...