setTimout problem

J

jackwootton

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
 
J

jackwootton

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.
 
D

dave

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,

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
 
R

RobG

On Jul 5, 10:50 am, "(e-mail address removed)" <[email protected]>
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 >
 
J

jackwootton

On Jul 5, 6:53 am, "(e-mail address removed)" <[email protected]>
wrote:
On Jul 5, 10:50 am, "(e-mail address removed)" <[email protected]>
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>


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top