Skip to main content
GameDev.net gamedev.net
🔒 Locked

[web] Running functions simultaneously in JavaScript

Started by SippyCup Jul 20, 2006 at 9:30 PM 6 replies 6.8k views
Original Post
SippyCup
SippyCup
Is it possible to run two &#106avascript 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 &#106avascript 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.
LessBread
LessBread
It's been a while since I've used &#106avascript, but iirc, to run two functions simultaneously you need to use a timer function, SetInterval or something like that.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
SippyCup
SippyCup
Cool, thanks. I think I'm using one or both of those already, so hopefully it won't be a big pain to fix.
markr
markr
Most browsers will only run &#106avascript code in one thread at a time, or perhaps the same thread.<br/><br/>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.<br/><br/>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.<br/><br/>Either use closures, or attach any relevant variables as properties to the elements in question.<br/><br/>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.<br/><br/>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.<br/><br/>The mouse over / mouse out events could then simply add them to the list and set the appropriate flag for fade in / out.<br/><br/>Mark
SippyCup
SippyCup
Hm.. Yep. You're right. :) Looks like I'm going to be redesigning this a little bit.
SippyCup
SippyCup
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]
Mathachew
Mathachew
Quote:
Original post by Anonymous Poster
i want to run 2 &#106avascript functions simultaneously for loading 2 different pages.one for each page<br/>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....<br/><br/>how do i run 2 scripts simultaneoulsy????<br/><br/><br/>thank u!<!--QUOTE--></td></tr></table></blockquote><!--/QUOTE--><!--ENDQUOTE--><br/><br/>Refer to the comments by markr.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.