setTimeout and passing argument

  • Thread starter Raphael Bauduin
  • Start date
R

Raphael Bauduin

Hi,

I'm working with mozilla, building a little XUL app.
I have a datasource, and want to display a message
once it is loaded (datasource.loaded is true).

So, I thought to use timouts, but as the code to execute is
passed as a string, I cannot pass local variable to this code.

Anyone has a suggestion?

Thanks.

Raph

Here is what I wanted to do (but I can't pass the datasource as argument):

setTimeout(check_if_loaded(datasource), 500)


with this function:

function check_if_loaded(a_ds)
{
if (!datasource.loaded)
{
setTimeout(check_if_loaded(datasource), 500)
}


}
 
J

Jim Ley

I'm working with mozilla, building a little XUL app.
I have a datasource, and want to display a message
once it is loaded (datasource.loaded is true).

I imagine it'll throw an event at such a time, catch that event, don't
test datasource.loaded is true at intervals, in fact if the technology
does require you to do this, walk away now, the technology is flawed
it should be event driven.

Jim.
 
R

Raphael Bauduin

Jim said:
I imagine it'll throw an event at such a time, catch that event, don't
test datasource.loaded is true at intervals, in fact if the technology
does require you to do this, walk away now, the technology is flawed
it should be event driven.

The event thing is what I'm looking at right now, but I still wonder how
to pass arguments to code called by a timeout.

Thanks for your remark anyway ;-)

Raph
 
R

Richard Cornford

The event thing is what I'm looking at right now, but I still
wonder how to pass arguments to code called by a timeout.

Some setTimeout implementations, probably including Mozilla's (as
Netscape 4 implemented it) will, when passed a function reference as
their first argument, accept arguments 3 to n as additional parameters
and pass these parameters on to the function referenced in the first
argument when it is called.

Richard.
 
R

Rich Persaud

Raphael Bauduin said:
Hi,

I'm working with mozilla, building a little XUL app.
I have a datasource, and want to display a message
once it is loaded (datasource.loaded is true).

So, I thought to use timouts, but as the code to execute is
passed as a string, I cannot pass local variable to this code.

Anyone has a suggestion?

There are more than a few caveats to using setTimeout. Some reading
material:

http://devedge.netscape.com/toolbox/examples/2003/CCallWrapper/
http://www.intranetjournal.com/articles/200010/js11.html
http://w3future.com/html/stories/callbacks.xml
http://blogs.gotdotnet.com/ericli/commentview.aspx/f249659d-a3f1-4f6b-9ca3-3bbcc2df240e
http://jsindex.com (search for 'multi-threading')

In general, you can create an anonymous function (closure) without
arguments that retains access to a local variable (like datasource)
from the enclosing lexical scope. The anonymous function can pass the
variable as a parameter to the function of your choice.

Note that Konqueror has (had?) a problem with anonymous functions and
setTimeout. Workarounds:

http://www.mojavelinux.com/forum/viewtopic.php?t=135&sid=55d2c5b33193fe8ec5387ab3c1d8dbcb
http://autometa.com/DOM
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top