Sign in to follow this  
SippyCup

[web] Running functions simultaneously in JavaScript

Recommended Posts

Is it possible to run two javascript functions simultaneously? I've written a fader script that is designed to work with most HTML elements. There are a few major functions involved, but when you get down to it, it's a fade in function and a fade out function. The problem I'm having is when I have two elements using the script. When you mouse over an element, it fades in. When you mouse away from it, it begins to fade out. If you then mouse over a second element, the first one ceases to fade out and gets stuck at its current opacity. In order to get it to go back to 0 opacity, you have to mouse over it again. Since there is no javascript error, I assume this is happening because I can't run two functions simultaneously. Would moving the fade out function to a different script solve the problem? Thanks in advance.

Share this post


Link to post
Share on other sites
It's been a while since I've used javascript, but iirc, to run two functions simultaneously you need to use a timer function, SetInterval or something like that.

Share this post


Link to post
Share on other sites
Most browsers will only run javascript code in one thread at a time, or perhaps the same thread.

The problem is not that you can't run two functions simultaneously (even though you typically can't), it's that you're not constructing your fade in / out script in a way which correctly works for several elements.

You're probably using a global (i.e. window) variable to store the fade in / out parameters, and remember which element it is. This way won't work with more than one element.

Either use closures, or attach any relevant variables as properties to the elements in question.

The way I'd do it is have an interval timer continuously running doing nothing very much, checking a global object to determine which things need to currently fade in / out and how far they've got.

Each tick, you fade each fading item either in or out as necessary, removing them from the list (probably really you'd use an object as an associative array) once they reach fully faded in our out.

The mouse over / mouse out events could then simply add them to the list and set the appropriate flag for fade in / out.

Mark

Share this post


Link to post
Share on other sites
After reading a little about closures, I think one all-knowing, all-seeing timer seems to be the best design. :)


Edit: That worked. Took me all of about 5 minutes to think it through and convert it.

[Edited by - SippyCup on July 26, 2006 7:41:51 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i want to run 2 javascript functions simultaneously for loading 2 different pages.one for each page
the 2 jscripts load pages. the first one is a video ...so b4 teh video gets over teh second has to be loaded....so that the user doenst have to wait for the second page to be loaded "after" the video is over....

how do i run 2 scripts simultaneoulsy????


thank u!

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
i want to run 2 javascript functions simultaneously for loading 2 different pages.one for each page
the 2 jscripts load pages. the first one is a video ...so b4 teh video gets over teh second has to be loaded....so that the user doenst have to wait for the second page to be loaded "after" the video is over....

how do i run 2 scripts simultaneoulsy????


thank u!


Refer to the comments by markr.

Share this post


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

Sign in to follow this