By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,395 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

Calling window.setInterval within a loop containing args

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a

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.