Jump to content
kody

How do I delay code from executing?

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...