Calling window.setInterval within a loop containing args

S

shawn

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), 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
 
T

Touffy

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), 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);
}

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 ?
 
V

VK

shawn said:
window.setInterval(InitMap(uuidValues), 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 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.
 
S

shawn

Touffy said:
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), 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);
}

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 ?


Thanks David, that worked a treat. :)
 

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
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top