setTimout problem

Discussion in 'Javascript' started by jackwootton@gmail.com, Jul 5, 2007.

  1. Guest

    Hello,

    I have a for loop, which calls a method

    for (var i = 0; i < GmailXChat.gmailChatFrames.length; i ++)
    {
    // myObjectArray.constructIframe();
    }

    the method 'constructIframe();' invloves constructing an HTML iframe,
    adding various elements to it and displaying it on a page. When
    constructing an iframe and adding it to a page dynamically, it is
    necessary to use setTimeout('finishIframe()',0) to complete the
    iframe. Where 'finishIframe()' adds any content to the iframe
    itself. This must be done since it stops the browser from attempting
    to add content to the iframe before any of the elements to be added
    have been created.

    This all works fine if the loop on cycles only once. However if the
    loop cycles more than once the following problem occurs:

    Where the call 'setTimeout('finishIframe()',0)' occurs, control is
    returned (to the loop), so the loop cycles round and calls
    myObjectArray.constructIframe() again, before the original call to
    myObjectArray.constructIframe() is actually completed.

    I need a call to myObjectArray.constructIframe() to complete fully
    (including the call to setTimeout('finishIframe()',0)), before control
    is returned to the loop, and the loop cycles.

    Hope someone can help, have been stuck on this for quite a while.

    Many thanks,

    Jack
     
    , Jul 5, 2007
    #1
    1. Advertising

  2. Guest

    On Jul 5, 10:50 am, "" <>
    wrote:
    > Hello,
    >
    > I have a for loop, which calls a method
    >
    > for (var i = 0; i < GmailXChat.gmailChatFrames.length; i ++)
    > {
    > // myObjectArray.constructIframe();
    >
    > }
    >
    > the method 'constructIframe();' invloves constructing an HTML iframe,
    > adding various elements to it and displaying it on a page. When
    > constructing an iframe and adding it to a page dynamically, it is
    > necessary to use setTimeout('finishIframe()',0) to complete the
    > iframe. Where 'finishIframe()' adds any content to the iframe
    > itself. This must be done since it stops the browser from attempting
    > to add content to the iframe before any of the elements to be added
    > have been created.
    >
    > This all works fine if the loop on cycles only once. However if the
    > loop cycles more than once the following problem occurs:
    >
    > Where the call 'setTimeout('finishIframe()',0)' occurs, control is
    > returned (to the loop), so the loop cycles round and calls
    > myObjectArray.constructIframe() again, before the original call to
    > myObjectArray.constructIframe() is actually completed.
    >
    > I need a call to myObjectArray.constructIframe() to complete fully
    > (including the call to setTimeout('finishIframe()',0)), before control
    > is returned to the loop, and the loop cycles.
    >
    > Hope someone can help, have been stuck on this for quite a while.
    >
    > Many thanks,
    >
    > Jack


    To add some more information, to me it is like JavaScript becomes
    multi threaded. Where a separate thread is started the moment the
    call setTimeout('finishIframe()',0) is made, and therefore the
    original thread returns to the loop. I know JavaScript is not multi
    threaded though.
     
    , Jul 5, 2007
    #2
    1. Advertising

  3. dave Guest

    On Jul 5, 6:53 am, "" <>
    wrote:
    > On Jul 5, 10:50 am, "" <>
    > wrote:
    >
    > > Hello,

    >
    > > I have a for loop, which calls a method

    >
    > > for (var i = 0; i < GmailXChat.gmailChatFrames.length; i ++)
    > > {
    > > // myObjectArray.constructIframe();

    >
    > > }

    >
    > > the method 'constructIframe();' invloves constructing an HTML iframe,
    > > adding various elements to it and displaying it on a page. When
    > > constructing an iframe and adding it to a page dynamically, it is
    > > necessary to use setTimeout('finishIframe()',0) to complete the
    > > iframe. Where 'finishIframe()' adds any content to the iframe
    > > itself. This must be done since it stops the browser from attempting
    > > to add content to the iframe before any of the elements to be added
    > > have been created.

    >
    > > This all works fine if the loop on cycles only once. However if the
    > > loop cycles more than once the following problem occurs:

    >
    > > Where the call 'setTimeout('finishIframe()',0)' occurs, control is
    > > returned (to the loop), so the loop cycles round and calls
    > > myObjectArray.constructIframe() again, before the original call to
    > > myObjectArray.constructIframe() is actually completed.

    >
    > > I need a call to myObjectArray.constructIframe() to complete fully
    > > (including the call to setTimeout('finishIframe()',0)), before control
    > > is returned to the loop, and the loop cycles.

    >
    > > Hope someone can help, have been stuck on this for quite a while.

    >
    > > Many thanks,

    >
    > > Jack

    >
    > To add some more information, to me it is like JavaScript becomes
    > multi threaded. Where a separate thread is started the moment the
    > call setTimeout('finishIframe()',0) is made, and therefore the
    > original thread returns to the loop. I know JavaScript is not multi
    > threaded though.


    The concept is the same though, when you a setTimeout() it does an
    asynchronous function call to finishIframe, then executes the next
    line of code
    In other words, your JS makes a call to finishIframe then executes the
    next line. The function finishIframe can do what ever work it needs to
    do, but your main JS code is also being executed at the same time
     
    dave, Jul 5, 2007
    #3
  4. RobG Guest

    On Jul 6, 5:04 am, dave <> wrote:
    > On Jul 5, 6:53 am, "" <>
    > wrote:
    >
    >
    >
    > > On Jul 5, 10:50 am, "" <>
    > > wrote:

    >
    > > > Hello,

    >
    > > > I have a for loop, which calls a method

    >
    > > > for (var i = 0; i < GmailXChat.gmailChatFrames.length; i ++)
    > > > {
    > > > // myObjectArray.constructIframe();

    >
    > > > }

    >
    > > > the method 'constructIframe();' invloves constructing an HTML iframe,
    > > > adding various elements to it and displaying it on a page. When
    > > > constructing an iframe and adding it to a page dynamically, it is
    > > > necessary to use setTimeout('finishIframe()',0) to complete the
    > > > iframe. Where 'finishIframe()' adds any content to the iframe
    > > > itself. This must be done since it stops the browser from attempting
    > > > to add content to the iframe before any of the elements to be added
    > > > have been created.

    >
    > > > This all works fine if the loop on cycles only once. However if the
    > > > loop cycles more than once the following problem occurs:

    >
    > > > Where the call 'setTimeout('finishIframe()',0)' occurs, control is
    > > > returned (to the loop), so the loop cycles round and calls
    > > > myObjectArray.constructIframe() again, before the original call to
    > > > myObjectArray.constructIframe() is actually completed.

    >
    > > > I need a call to myObjectArray.constructIframe() to complete fully
    > > > (including the call to setTimeout('finishIframe()',0)), before control
    > > > is returned to the loop, and the loop cycles.

    >
    > > > Hope someone can help, have been stuck on this for quite a while.

    >
    > > > Many thanks,

    >
    > > > Jack

    >
    > > To add some more information, to me it is like JavaScript becomes
    > > multi threaded. Where a separate thread is started the moment the
    > > call setTimeout('finishIframe()',0) is made, and therefore the
    > > original thread returns to the loop. I know JavaScript is not multi
    > > threaded though.


    Control never leaves the loop in the first place. The call to
    setTimeout establishes a function that will run later. As soon at
    that call returns, the loop continues. Whatever was assigned to
    setTimeout doesn't run until after the main funciton is complete.


    > The concept is the same though, when you a setTimeout() it does an
    > asynchronous function call to finishIframe, then executes the next
    > line of code


    No, it makes a call to window.setTimout which will wait for the end of
    the function, then wait for the timeout period, then execute whatever
    function it was passed - it is all completely synchronous.


    > In other words, your JS makes a call to finishIframe then executes the
    > next line. The function finishIframe can do what ever work it needs to
    > do, but your main JS code is also being executed at the same time


    The call to setTimeout establishes a function that will run completely
    asynchronously.

    <URL: http://developer.mozilla.org/en/docs/DOM:window.setTimeout >


    --
    Rob
     
    RobG, Jul 6, 2007
    #4
  5. Guest

    On Jul 5, 7:26 pm, RobG <> wrote:
    > On Jul 6, 5:04 am, dave <> wrote:
    >
    >
    >
    > > On Jul 5, 6:53 am, "" <>
    > > wrote:

    >
    > > > On Jul 5, 10:50 am, "" <>
    > > > wrote:

    >
    > > > > Hello,

    >
    > > > > I have a for loop, which calls a method

    >
    > > > > for (var i = 0; i < GmailXChat.gmailChatFrames.length; i ++)
    > > > > {
    > > > > // myObjectArray.constructIframe();

    >
    > > > > }

    >
    > > > > the method 'constructIframe();' invloves constructing an HTML iframe,
    > > > > adding various elements to it and displaying it on a page. When
    > > > > constructing an iframe and adding it to a page dynamically, it is
    > > > > necessary to use setTimeout('finishIframe()',0) to complete the
    > > > > iframe. Where 'finishIframe()' adds any content to the iframe
    > > > > itself. This must be done since it stops the browser from attempting
    > > > > to add content to the iframe before any of the elements to be added
    > > > > have been created.

    >
    > > > > This all works fine if the loop on cycles only once. However if the
    > > > > loop cycles more than once the following problem occurs:

    >
    > > > > Where the call 'setTimeout('finishIframe()',0)' occurs, control is
    > > > > returned (to the loop), so the loop cycles round and calls
    > > > > myObjectArray.constructIframe() again, before the original call to
    > > > > myObjectArray.constructIframe() is actually completed.

    >
    > > > > I need a call to myObjectArray.constructIframe() to complete fully
    > > > > (including the call to setTimeout('finishIframe()',0)), before control
    > > > > is returned to the loop, and the loop cycles.

    >
    > > > > Hope someone can help, have been stuck on this for quite a while.

    >
    > > > > Many thanks,

    >
    > > > > Jack

    >
    > > > To add some more information, to me it is like JavaScript becomes
    > > > multi threaded. Where a separate thread is started the moment the
    > > > call setTimeout('finishIframe()',0) is made, and therefore the
    > > > original thread returns to the loop. I know JavaScript is not multi
    > > > threaded though.

    >
    > Control never leaves the loop in the first place. The call to
    > setTimeout establishes a function that will run later. As soon at
    > that call returns, the loop continues. Whatever was assigned to
    > setTimeout doesn't run until after the main funciton is complete.
    >
    > > The concept is the same though, when you a setTimeout() it does an
    > > asynchronous function call to finishIframe, then executes the next
    > > line of code

    >
    > No, it makes a call to window.setTimout which will wait for the end of
    > the function, then wait for the timeout period, then execute whatever
    > function it was passed - it is all completely synchronous.
    >
    > > In other words, your JS makes a call to finishIframe then executes the
    > > next line. The function finishIframe can do what ever work it needs to
    > > do, but your main JS code is also being executed at the same time

    >
    > The call to setTimeout establishes a function that will run completely
    > asynchronously.
    >
    > <URL:http://developer.mozilla.org/en/docs/DOM:window.setTimeout>
    >
    > --
    > Rob


    I solved my problem. I didn't realise setTimeout could accept a
    function pointer as well as a function name as a string. This worked
    for me since it didn't matter what order events were executed in, as
    long as the completed eventually. The function called with setTimeout
    does indeed run after the main loop has completed. All the setTimeout
    calls are then executed.

    Many thanks for your help.

    Jack
     
    , Jul 6, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Suurkuusk
    Replies:
    0
    Views:
    4,016
    Andreas Suurkuusk
    Jul 27, 2003
  2. Ted Miller
    Replies:
    0
    Views:
    5,199
    Ted Miller
    Sep 13, 2003
  3. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    568
    Bullschmidt
    May 11, 2004
  4. Vincent M.

    setTimout error

    Vincent M., Apr 5, 2004, in forum: Javascript
    Replies:
    2
    Views:
    82
    Michael Winter
    Apr 5, 2004
  5. anagai

    canceling settimout altogether

    anagai, Jan 4, 2006, in forum: Javascript
    Replies:
    2
    Views:
    78
    Thomas 'PointedEars' Lahn
    Jan 4, 2006
Loading...

Share This Page