Image Preload With A For Loop

  • Thread starter X l e c t r i c
  • Start date
X

X l e c t r i c

This isn't working and I can't figure out what I'm doing wrong:

var c_imgs = new Array(
'images/champascari.jpg',
'images/champfangio.jpg',
'images/champhawthorn.jpg',
'images/champhill.jpg',
'images/champsurtees.jpg',
'images/champlauda.jpg',
'images/champschumacher.jpg');
var pre_img;
for (var i = 0; i < c_imgs.length; i++)
{
pre_img = new Image();
pre_img.src = c_imgs;
}

If this code is correct, should I put it in a function and then call for
it ?

This is the page it's on:
http://www.xlectric.com/Ferrari/champs.html

Later, Art.
 
E

Evertjan.

X l e c t r i c wrote on 08 dec 2005 in comp.lang.javascript:
This isn't working and I can't figure out what I'm doing wrong:

var c_imgs = new Array(
'images/champascari.jpg',
'images/champfangio.jpg',
'images/champhawthorn.jpg',
'images/champhill.jpg',
'images/champsurtees.jpg',
'images/champlauda.jpg',
'images/champschumacher.jpg');
var pre_img;

var pre_img = new Array();
for (var i = 0; i < c_imgs.length; i++)
{
pre_img = new Image();
pre_img.src = c_imgs;
}

If this code is correct, should I put it in a function and then call for
it ?

This is the page it's on:
http://www.xlectric.com/Ferrari/champs.html

Later, Art.
 
X

X l e c t r i c

Evertjan wrote:

"var pre_img = new Array();"

Thank you Evertjan, it works great. I appreciate your help.

Is that pre_img array for the preload only ? For example if I want to
call for an image later on using the array, would I use the original
c_imgs array, like this (for the first image in the array):

c_imgs[0]

or would I use:

pre_img[0]

Once again, thanks.

Later, Art.
 
L

Lasse Reichstein Nielsen

This isn't working and I can't figure out what I'm doing wrong:

var c_imgs = new Array(
'images/champascari.jpg',
'images/champfangio.jpg',
'images/champhawthorn.jpg',
'images/champhill.jpg',
'images/champsurtees.jpg',
'images/champlauda.jpg',
'images/champschumacher.jpg');
var pre_img;

pre_img is uninitialized, so it contains the value "undefined".
for (var i = 0; i < c_imgs.length; i++)
{
pre_img = new Image();


Here you try to find the 0th property of it, which fails with an
error.

You should make sure to show all errors while developing a
page. Depending on which browser you use, the way to show errors
differ (in IE, it's somewher in advanced internet properties, in
Mozilla/Netscape based browsers, enter "javascript:" in the address
bar).

If this code is correct, should I put it in a function and then call for
it ?

If it should be executed right then and there, there is no reason to
wrap it in a function, except perhaps to hide the local variables ...
which is actually reason enough, so yes, do that.

/L
 
X

X l e c t r i c

Lasse Reichstein Nielsen wrote:

"pre_img is uninitialized, so it contains the value "undefined".

for (var i = 0; i < c_imgs.length; i++)
{
pre_img = new Image();

Here you try to find the 0th property of it, which fails with an error.

You should make sure to show all errors while developing a page.
Depending on which browser you use, the way to show errors differ (in
IE, it's somewher in advanced internet properties, in Mozilla/Netscape
based browsers, enter "javascript:" in the address bar).

If this code is correct, should I put it in a function and then call for
it ?

If it should be executed right then and there, there is no reason to
wrap it in a function, except perhaps to hide the local variables ...
which is actually reason enough, so yes, do that."

I realize that now since Evertjan pointed out the use of the array.

I appreciate you teaching me how to show errors in the browsers, I did
not know how to do that.

Thank you very much for your help.

Later, Art.
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top