Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setInterval - trouble passing array

Status
Not open for further replies.

SaRiD

Programmer
Apr 19, 2003
45
0
0
Hi

I'm trying to use the setInterval function. I seem to be having trouble passing an array through to the function.

Lets say my array is
var my_array = Array("red", "green", "blue");

if I use the following:
setInterval("my_function(my_array)", 50)
It errors saying "my_array is undefined"
If I put quotes around my_array it then passes through the contents of "my_array" as a string and not an array like I wanted.

Any thoughts???
 
Declare var my_array outside of the function for the purpose of making it global scope. Isolating the essential in a demo, like this.
[tt]
<script>
var my_array;
function my_function(a){
alert(a[1]);
}
window.onload=function() {
my_array = Array("red", "green", "blue");
//setInterval("my_function(my_array)",50);
setTimeout("my_function(my_array)",50);
}
</script>
[/tt]
I use setTimeout() for the demo, else, setInterval() would be annoying.
 
Agreed that would normally do the job, except in this case I wont be able to use a global variable.

The script that I'm using is a slideshow script. The script will work fine, with global variables, as long as there is only one slideshow on the screen at a time. I'm planning on using a few slideshows on the same page, which means the global variable needs to be unique for each slideshow.

I have the whole script working except for this one little issue.

Any other thoughts/ideas?
 
If you like... and that slideshows are that different...
[tt] setInterval("my_function(['"+my_array.join("','")+"'])", 50)[/tt]
and then you say you can do better than a heck.
 
And just to add to this little pickle I'm in... its a 2 dimensional array :)
 
Make a global three dimensional array, store them all in the same global array with a differant first index, pass the first level index as an arg instead?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top