467,907 Members | 1,114 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,907 developers. It's quick & easy.

Calling window.setInterval within a loop containing args

Hi All

Was trying to get this bit of code working (simplified of course)

for (i=0;i<uuid_num;i++) {
window.setInterval(InitMap(uuidValues[i]), timePeriod);
}

Where uuid_num, uuidValues, timePeriod are defined above, along with
function InitMap.

Now, guess I have two questions,

1. Can I pass args within the window.setInterval,
2. Can I call this window.setInterval several times with different args
based on my for loop.

Or is there another approach to this that someone can see?

Regards

Shawn

Sep 28 '06 #1
  • viewed: 5432
Share:
3 Replies
On 2006-09-28 15:06:39 +0200, "shawn" <sh*********@gmail.comsaid:
Hi All

Was trying to get this bit of code working (simplified of course)

for (i=0;i<uuid_num;i++) {
window.setInterval(InitMap(uuidValues[i]), timePeriod);
}

Where uuid_num, uuidValues, timePeriod are defined above, along with
function InitMap.

Now, guess I have two questions,

1. Can I pass args within the window.setInterval,
yes. But not this way.

setInterval requires a function as its first argument, not a function
call (unless it returns a function). So you can't put it the way you
dit.
However, setInterval (and setTimeout) can take additional arguments
after the function (first arg) and the interval (or timeout) delay
(second arg). These additional arguments will be passed, in the same
order, to the function.
So this should work :

for (i=0;i<uuid_num;i++) {
window.setInterval(InitMap, timePeriod, uuidValues[i]);
}

Alternatively, setInterval can accept a string as its first argument,
and will eval() the string every tick. So you could construct a
funciton call as a string, including the arguments. But, as some are
wont to say, "eval is evil", so just forget I said anything about
passing a string to setInterval.
2. Can I call this window.setInterval several times with different args
based on my for loop.
Of course. The arguments are evaluated every time you call setInterval
(but not every time the interval'd function itself is called) so every
interval you set here will have its own uuidValue.
Or is there another approach to this that someone can see?
You should think on why you need to set so many parallel intervals,
though. Maybe a single interval could do the job more efficiently, and
you could put the for() loop inside the interval'd function ?

--
David Junger

Sep 28 '06 #2
VK

shawn wrote:
window.setInterval(InitMap(uuidValues[i]), timePeriod);
setTimeout and setInterval for the first argument take only:
a) a string to be avaluated after timePeriod
b) a reference to a function to be executed

So in the code above you do: "execute function InitMap with argument
uuidValues[i] and use the result as a reference to the function to be
executed every timePeriod".

That is not of course what you wanted: so concatenate a string to
evaluate instead:

window.setInterval("InitMap(uuidValues[" + i + "]"), timePeriod);

As a side note: setInterval is a rather dangerous stuff for operations
with unpredictable length, say involving client-server communications.
With short timePeriod and long function execution you can create
multiple function instances and make a memory mess. It is usually
better to setTimeout instead, let the function to do the stuff in peace
and set new timeout before exit.

Sep 28 '06 #3

Touffy wrote:
On 2006-09-28 15:06:39 +0200, "shawn" <sh*********@gmail.comsaid:
Hi All

Was trying to get this bit of code working (simplified of course)

for (i=0;i<uuid_num;i++) {
window.setInterval(InitMap(uuidValues[i]), timePeriod);
}

Where uuid_num, uuidValues, timePeriod are defined above, along with
function InitMap.

Now, guess I have two questions,

1. Can I pass args within the window.setInterval,

yes. But not this way.

setInterval requires a function as its first argument, not a function
call (unless it returns a function). So you can't put it the way you
dit.
However, setInterval (and setTimeout) can take additional arguments
after the function (first arg) and the interval (or timeout) delay
(second arg). These additional arguments will be passed, in the same
order, to the function.
So this should work :

for (i=0;i<uuid_num;i++) {
window.setInterval(InitMap, timePeriod, uuidValues[i]);
}

Alternatively, setInterval can accept a string as its first argument,
and will eval() the string every tick. So you could construct a
funciton call as a string, including the arguments. But, as some are
wont to say, "eval is evil", so just forget I said anything about
passing a string to setInterval.
2. Can I call this window.setInterval several times with different args
based on my for loop.

Of course. The arguments are evaluated every time you call setInterval
(but not every time the interval'd function itself is called) so every
interval you set here will have its own uuidValue.
Or is there another approach to this that someone can see?

You should think on why you need to set so many parallel intervals,
though. Maybe a single interval could do the job more efficiently, and
you could put the for() loop inside the interval'd function ?

--
David Junger
Thanks David, that worked a treat. :-)

Sep 28 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Richard A. DeVenezia | last post: by
4 posts views Thread by barry | last post: by
26 posts views Thread by yb | last post: by
4 posts views Thread by alexandre.brisebois | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.