Topic: how does set time out work? (Page 1 of 1) |
|
---|---|
Neurotic (0) Inmate Newly admitted From: |
posted 05-25-2006 12:25
Hi, I'm designing a site that is going to have an application feel to it, and to do that i wrote transitions such as fade in and fade out. Unfortunetaly I can get it to work if i use setTimeout to specific time intervals, ie after fade out after 1.5 seconds fade in, but I would like not to have to do this. I am pulling html from another page using a get request that is working, that is my _Ajax.getHTMLFrom code: _transition = {}; _transition.fadeIsDone = true; //public functions _transition.fadeInTo = function(page,fadeObjectId) { info = _Ajax.getHMLFrom(page); _transition.fade(true,fadeObjectId); // while(!_transition.fadeIsDone) ;//wait(); _transition.fadeTarget.innerHTML = info; _transition.fade(false,fadeObjectId); // while(!_transition.fadeIsDone); } _transition.fade = function(mode,fadeObjectId) { _transition.fadeIsDone = false; _transition.fadeTarget = document.getElementById(fadeObjectId); switch(mode) { case true: window.setTimeout("_transition._fade(true,100)",50); break; case false: window.setTimeout("_transition._fade(false,0)",50); break; default: } } //private functions //mode can be true, false, or TOGGLE _transition._fade = function(mode,opacity) { var fadeTarget = _transition.fadeTarget; switch(mode) { case true: fadeTarget.style.filter = "alpha(opacity="+opacity+")"; opacity-=10; if(opacity > 0) window.setTimeout("_transition._fade(true,"+opacity+")",500); //call func again else _transition.fadeIsDone = true; break; case false: fadeTarget.style.filter = "alpha(opacity="+opacity+")"; opacity+=10; if(opacity < 100) window.setTimeout("_transition._fade(false,"+opacity+")",500); else _transition.fadeIsDone = true; break; case "TOGGLE": break; default: alert("Will not fade with mode "+mode); } }
|
Maniac (V) Inmate From: |
posted 05-30-2006 10:21
Timing. The concept of spawning a new thread is somehow "correct", although these aren't real threads. |
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 05-30-2006 12:59
Or have a look at http://script.aculo.us - a neat little library that saves tons of time on such problems. |
Bipolar (III) Inmate From: Umeå, Sweden |
posted 05-30-2006 19:46
To understand how timers and events work in script, there's a few things you need to understand: |
Maniac (V) Inmate From: |
posted 05-30-2006 19:51
Oooohh.. interesting liorean. Thanks for sharing. |
Bipolar (III) Inmate From: Umeå, Sweden |
posted 05-31-2006 05:06
Well, JavaScript isn't really made for graphics manipulation anyways, even less animation. It doesn't have integer types and doesn't have real arrays for pixel manipulations; it doesn't have any APIs specifically geared towards graphics, be that vector or pixel (except for recent additions or non-browser hosts anyways); it doesn't have reliable timers (Win9x your best timer resolution is 55 ms, WinNT it's 10 ms for uniprocessor/unicore/non-hyperthreaded systems and between 10 and 16 ms for multiprocessor/multicore/hyperthreaded systems), it's unreliably scheduled because it depends on the browser host environment, it's low priority and thus gets hit by context switching fairly often, the only timer method that could be considered reliable is the busy wait loop, and if you have more than one timer running all bets are off on the timing - moz is especially bad at scheduling multiple timers but at least you can rely on it firing the timer events in the right order. |
Maniac (V) Inmate From: |
posted 05-31-2006 07:37
It all depends on what you think is best. In terms of being easilly usable and good learning material, |