Results 1 to 8 of 8

Thread: Delay

  1. #1
    Join Date
    Nov 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Delay

    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

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    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

  4. #4
    Join Date
    Nov 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    anyway thnx to all, this is the most logical answers in this site i got it

  5. #5
    Join Date
    Nov 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by mwinter View Post
    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'

  6. #6
    Join Date
    Nov 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by aalroubaie View Post
    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?

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by supernewb View Post
    Quote Originally Posted by mwinter View Post
    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.

    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.


    Quote Originally Posted by Twey
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •