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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preload in a loop?

Status
Not open for further replies.

davv

Programmer
Apr 18, 2001
10
SE
Hello programmers! I want to preload 60 images. There are 20 jpegs with 3 variations each. The images are named ovn1_niv1, ovn1_niv2, ovn1_niv3, ovn2_niv1... So instead of writing almost the same lines 60 times I want to preoload them with two loops. I hoped that this would work but it doesn’t. Anyone have any ideas?

for (i=1; i<=20; i++){
for (j=1; j<=3; j++){
eval(&quot;var ovn&quot;+i+&quot;niv&quot;+j+&quot; = new Image()&quot;);
eval(&quot;ovn&quot;+i+&quot;niv&quot;+j+&quot;.src&quot;) = eval(&quot;ovn&quot; +i+ &quot;/niv&quot; +j+ &quot;/ovn&quot;+i+&quot;_niv&quot;+j+&quot;.jpg&quot;);
}
}
 
Try this it loads the images into an array and doesnt use evals which always seem to behave oddly to me....

var myImg = new Array();
function preload(){
var count = 0;
for(i=0; i<60; i++) myImg = new Image();
for (j=1; j<21; j++){
for(k=1; k<4; k++){
var loc = &quot;ovn&quot;+j+&quot;niv&quot;+k+&quot;.jpg&quot;;
myImg[count].src = loc;
count++;
}
}
}

hope it helps

rob
 
Thanks very much! I didn't work att first, but I modified it a little bit...

function preload(){
var myImg = new Array();
var count = 0;
for(i=0; i<60; i++) {
myImg[ i ] = new Image();
}
for (j=1; j<21; j++){
for(k=1; k<4; k++){
var loc = &quot;ovn&quot; +j+ &quot;/niv&quot; +k+ &quot;/ovn&quot;+j+&quot;_niv&quot;+k+&quot;.jpg&quot;
myImg[count].src = loc;
count++;
}
}
}
 
test:



seems like [ i ] disapears after submitting post. probably why it ddint work at first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top