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