473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
3 5755
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Richard A. DeVenezia | last post by:
At line this.timerId = setInterval (function(){this.step()}, 100) I get error dialog Error:Object doesn't support this property or method. If I change it to this.timerId = setInterval...
3
by: Eli | last post by:
Hello all, I have just added a small piece of JavaScript code to a basic HTML page of mine. At the end of the page (after the closing </HTML>) I added: <SCRIPT LANGUAGE="JavaScript">...
6
by: marktm | last post by:
Hi- I am trying to use setInterval to call a method within an object and have not had any luck. I get "Object doesn't support this property or method" when I execute the following code. What I...
4
by: barry | last post by:
If The script below is exeuted from the onload="startit()" in the body tag everything works fine but when I remove the onload and try to execute by using the attributes.add from within the Page_Load...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
4
by: alexandre.brisebois | last post by:
Hi, I am using access 2003, I would like to know if there is an option to reorganize the tables in a maner that is readable, as we can do in sql sever 2000 or 2005. I have been given a database...
1
by: jsavagedesign | last post by:
This is a project I have been working on lately and I keep running into problems. The idea is to be able to attaching an animation to a movie clip and call it later. Or multiple animation at once. ...
1
by: jonahmason | last post by:
hi. a javascript newbie here in dire need of help. for the past 3 days i've been trying to get a script using setinterval to work but a part of the code, that executes after setinterval hits a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.