PDA

View Full Version : Delay



aalroubaie
11-16-2006, 01:35 PM
hi all,
question for javascripts programmers
i'm a beginner in javascript but not in programming by other langs and i'm using setTimeout to make delay in my animation for div block, i have four divs each one can expand and collapsed as a slide, the problem occur when more than one block move in the same time, and the reason because the seTimeout is a delay to execute a javascript code, function...etc and in this case the code execution will jump and execute what is after setTimeout, before end the execution of the script setTimeout

in another word, i want a way for delay that make the execution of the code stop until finish delay period and run the script that linked to the delay command. I donít know if there is another way not using setTimeout or setInterval.

i think javascript is make more than one thread in its execution, not as one thread. i tried to re-write my code, but no way, i thought about AJAX also but i hope to find a way to solve that in javascript itself.

best regards

Twey
11-16-2006, 06:38 PM
setTimeout() and setInterval() both create new threads. There's no way to prevent this. You simply have to stop processing after you've called one, then resuming processing in the new thread.

mwinter
11-16-2006, 08:39 PM
setTimeout() and setInterval() both create new threads.

Not within the browser. Browser scripting engines are single-threaded. The setTimeout and setInterval methods cause internal events to be raised, and then, at the next opportunity, the browser executes the given code. No interruptions, and no concurrency.

Mike

aalroubaie
11-17-2006, 05:14 AM
anyway thnx to all, this is the most logical answers in this site i got it

supernewb
11-22-2006, 10:50 AM
Not within the browser. Browser scripting engines are single-threaded. The setTimeout and setInterval methods cause internal events to be raised, and then, at the next opportunity, the browser executes the given code. No interruptions, and no concurrency.

Mike

I don't believe this is necessarily true, esp. for a modern browser like IE and the setTimeout function. Please refer: http://www.oreillynet.com/cs/user/view/cs_msg/81542 and by googling 'multithread javascript browsers'

supernewb
11-22-2006, 10:52 AM
in another word, i want a way for delay that make the execution of the code stop until finish delay period and run the script that linked to the delay command. I donít know if there is another way not using setTimeout or setInterval.


Try Mad4Milk's moofx script and its 'Accordian' Feature: http://moofx.mad4milk.net/. Load that site - is that what you are looking for in terms of functionality?

Twey
11-22-2006, 06:10 PM
I don't believe this is necessarily true, esp. for a modern browser like IE and the setTimeout function.According to your link, this is untrue only for IE, and probably only for IE/Win at that: I doubt IE/Mac would use COM objects.

At any rate, this apparently applies to "AJAX" only because the XMLHttpRequest object is implemented as a COM object. setTimeout() and setInterval() should not trigger the creation of a new thread.

mwinter
11-22-2006, 10:11 PM
Browser scripting engines are single-threaded.

I don't believe this is necessarily true,

It is, and you could test it yourself, quite easily.



esp. for a modern browser like IE and the setTimeout function.

Browsers are multi-threaded, and have been for a long time. The setTimeout function, however, does not involve threading the scripting engine, though.



Please refer: http://www.oreillynet.com/cs/user/view/cs_msg/81542 ...

Perhaps you'd actually like to read that, especially the comments at the link provided in the first comment. The single-thread nature of client-side scripts is well-known.




According to your link, this is untrue only for IE, and probably only for IE/Win at that: I doubt IE/Mac would use COM objects.

Even then, that isn't threading in the script engine itself. The XMLHttpRequest COM object is obviously asynchronous, but COM objects are host objects and a different kettle of fish. Even though the setTimeout and setInterval functions are host objects, they are demonstrably synchronous, insofar as they do not cause the code passed to them to run concurrently with other script code. The same is true of event listeners, include those executed in relation to XMLHttpRequest events.

Client-side scripts have no thread management code, so it would be reckless to invite race conditions and other thread-related issues with little in the way of means to handle them.

Mike