Forum Moderators: open

Message Too Old, No Replies

Only running a function if it's not already running

setTimeout() problems

         

Nutter

5:22 pm on Jul 1, 2005 (gmt 0)

10+ Year Member



I have a function that fades a set of images out, swaps them, and fades the back in when the visitor clicks on an arrow. I use a series of setTimeout() calls for this. How can I make it so that if the process is active then clicking the arrow won't start it again?

Thanks,
- Ryan

icpooreman

8:50 pm on Jul 1, 2005 (gmt 0)

10+ Year Member



Just set a boolean value for whether or not its complete and check it before you run the swap function.

Nutter

8:52 pm on Jul 1, 2005 (gmt 0)

10+ Year Member



Do you mind expanding on that? I tried what I think you're describing. At the top of the js, var bRunning = false. Top of function bRunning = true;. Bottom of function, bRunning = false;. Before anything else, if (bRunning==true) {return; }

icpooreman

9:13 pm on Jul 1, 2005 (gmt 0)

10+ Year Member



well ya I'm a little vague on what you're trying to accomplish but if you've already got a function that works to fade the image in and out just have a global variable set it originally to false then

function(){

if{var!=true){
your function as you have in now
var=true;
}
}

the only tricky part is if you want to set the var back equal to false after it's done and how you do that depends on how your function runs

Nutter

12:25 am on Jul 2, 2005 (gmt 0)

10+ Year Member



Warning: Rookie question ahead :)

To make a global variable, do I just have 'var whatever=false;' somewhere outside a function, probably near the top of the file? Or is there more to it than that?

Nutter

12:40 am on Jul 2, 2005 (gmt 0)

10+ Year Member



Never mind, I figured it out. Thanks for your help.

Here's what I was doing wrong. I had a series of setTimeout() functions that called sub functions to handle the fading and switch images at the correct time. But I forgot to use setTimeout() to change the global variable. When I did that, it worked perfectly.

Thanks again.