473,386 Members | 1,738 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,386 software developers and data experts.

preloading images

Hi Gurus

Preloading images has got to be JS 101. However, it does not seem to be
working. Here is the function that I am using. I added the alerts to make
sure it is working and all the right alerts show up, yet when I do my
mouseover, it still takes about a second (only the first time) to load the
image (thumbnail).

Am I missing anything?

loadims(max, letter) {//loads information images
if (document.images) {
max++;
alert(max);
for(i=1; i < max; i++) {
rslt = new Image();
imgn = 'm/' + letter + i + '.jpg';
rslt.src = imgn
alert(imgn);
}
loaded = true;
}
}
TIA

nicolaas
Aug 29 '05 #1
2 1814
Hello!

windandwaves wrote:
for(i=1; i < max; i++) {
rslt = new Image();
imgn = 'm/' + letter + i + '.jpg';
rslt.src = imgn


I think, instead of loading into a transient image object you should
rather preload the images into seperate and persistent image objects.

Did you try making rslt a global Array of Images?

Cheers
Daniel

PS: Giving an URL that links to an example on the net is always helpful.
Aug 29 '05 #2
Ivo
"windandwaves" wrote

Preloading images has got to be JS 101. However, it does not seem to be
working. Here is the function that I am using. I added the alerts to
make sure it is working and all the right alerts show up, yet when I do my
mouseover, it still takes about a second (only the first time) to load the
image (thumbnail).

Am I missing anything?

loadims(max, letter) {//loads information images
Presumably the keyword "function" slipped off the clipboard here.
if (document.images) {
This mistake is classic. Testing for "document.images" when "window.Image"
is the feature you are going to use.
max++;
alert(max);
for(i=1; i < max; i++) {
rslt = new Image();
imgn = 'm/' + letter + i + '.jpg';
rslt.src = imgn
alert(imgn);
}
loaded = true;
}
}


The variable that holds the image to load, is re-assigned to another image
in the very iteration of the loop. This leaves very little time for the
image to actually load. Try using unique variables for each preloaded image
instead, that way they can all load alongside eachother. Comapre this
preloading function:

var pr = []; // global variable
function preload() {
if( window.Image ) {
var p = arguments, i = p.length;
while(i--) {
pr[i] = new Image();
pr[i].src = p[i];
} } }

This would be called with the image URL's as arguments, as many as you like,
and all in one call to the function:

preload( 'example.gif', '/example.jpg', '../example.png' );

hth
ivo
http://4umi.com/web/javascript/

Aug 29 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Fabian | last post by:
var preload1 = new Image(); preload1.src = "/pic/yay.gif"; var preload2 = new Image(); preload2.src = "/pic/nay.gif"; The above is meant to preload image files, yes? Problem is, it doesnt seem...
2
by: Julie | last post by:
Hi, I'm trying to change images on a website without reloading the whole page and use the following code to preload the images: var preloadFlag = false; function preloadImages() { if...
6
by: michaaal | last post by:
Is this the correct way to preload an image...? Var Image1 = new Image() ....And then when I'm ready to use the image I can do this...? Button1.src=Image1.src ....Or am I just telling...
4
by: Dennis Allen | last post by:
Hi. I have one image that has to show up in a html page several times. To save time, I'd like to preload this image. As an example: if (document.images) { tester = new Image()...
40
by: Geoff Cox | last post by:
Hello, I am still having problems - apologies if the answer is in previous postings! I now have, in the header, <sctipt> var myimages=new Array();
2
by: sachaburnett | last post by:
Hi everyone! I'm new to Javascript and am finding so much useful information on this group, so thanks to you all! I have a question about preloading images for onmouseover/out effects and...
7
by: Inny | last post by:
Hello again, Im using the code below in a child page (popup), the images are called from the parent page. When the changer is running, the child page goes white between images. I realise this is...
7
by: Keith Hughitt | last post by:
Hi all, I am having trouble preloading images in a javascript application, and was wondering if anyone had any suggestions. Basically I have a bunch of images stored in a database as BLOBs. At...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.