Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > setTimout problem

Reply
Thread Tools

setTimout problem

 
 
jackwootton@gmail.com
Guest
Posts: n/a
 
      07-05-2007
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

 
Reply With Quote
 
 
 
 
jackwootton@gmail.com
Guest
Posts: n/a
 
      07-05-2007
On Jul 5, 10:50 am, "(E-Mail Removed)" <(E-Mail Removed)>
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.

 
Reply With Quote
 
 
 
 
dave
Guest
Posts: n/a
 
      07-05-2007
On Jul 5, 6:53 am, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> On Jul 5, 10:50 am, "(E-Mail Removed)" <(E-Mail Removed)>
> 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.


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

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      07-05-2007
On Jul 6, 5:04 am, dave <(E-Mail Removed)> wrote:
> On Jul 5, 6:53 am, "(E-Mail Removed)" <(E-Mail Removed)>
> wrote:
>
>
>
> > On Jul 5, 10:50 am, "(E-Mail Removed)" <(E-Mail Removed)>
> > 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

 
Reply With Quote
 
jackwootton@gmail.com
Guest
Posts: n/a
 
      07-06-2007
On Jul 5, 7:26 pm, RobG <(E-Mail Removed)> wrote:
> On Jul 6, 5:04 am, dave <(E-Mail Removed)> wrote:
>
>
>
> > On Jul 5, 6:53 am, "(E-Mail Removed)" <(E-Mail Removed)>
> > wrote:

>
> > > On Jul 5, 10:50 am, "(E-Mail Removed)" <(E-Mail Removed)>
> > > 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/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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
canceling settimout altogether anagai Javascript 2 01-04-2006 08:59 PM
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM
setTimout error Vincent M. Javascript 2 04-05-2004 06:10 PM



Advertisments