On Jul 6, 5:04 am, dave <dave.wa...@gmail.com> wrote:
> On Jul 5, 6:53 am, "jackwoot...@gmail.com" <jackwoot...@gmail.com>
> wrote:
>
>
>
> > On Jul 5, 10:50 am, "jackwoot...@gmail.com" <jackwoot...@gmail.com>
> > wrote:
>
> > > Hello,
>
> > > I have a for loop, which calls a method
>
> > > for (var i = 0; i < GmailXChat.gmailChatFrames.length; i ++)
> > > {
> > > // myObjectArray[i].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[i].constructIframe() again, before the original call to
> > > myObjectArray[i].constructIframe() is actually completed.
>
> > > I need a call to myObjectArray[i].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...dow.setTimeout >
--
Rob