/    Sign up×
Community /Pin to ProfileBookmark

Is there a reliable way to preload Images in memory?

Hi,

i’m working on a JS game project in my spare time. Works ok as long as it’s running local, but i get images loading problems as soon as i run the script over the net.

What i’m basically wanting to do is :
i use DIV elements with IMG inside

In JS, i define an array of Image objects (myElement = new Image()?

To make sure that my Image array is loaded before i use it, i check the “complete” property of each of the images.
I’m not even sur the “complete” property is supposed to indicate that the image is COMPLETEly loaded, but it seems so.
I was expecting an “isLoaded” property, but…

Anyway, when i want to switch the src of my document images with an src from my image array, the navigator will try to load that image from the net, and it will take *ages* (eventually it will never fully load in some cases).

If someone see what i means, i’d be more than happy to learn how to get rid of the problem!

In case you want to have a look at the (huge) script; here’s the URL to test it non-working over the net :
[url]http://cdecas.free.fr/dhtml/[/url]

and a zip file to test it locally (runs perfectly) :
[url]http://cdecas.free.fr/dhtml/newgame1.zip[/url]

(Developped for IE 5> and a fast PC! ? Will run on NS6, but slower and with more problems)

Thanks!!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@GollumAug 13.2003 — You can use the onload event on the Image object...

<i>
</i>function myImageHasLoaded()
{
// hurrah!
}
function myImageHasFailed()
{
// boo!
// try again...
this.src = this.source;
}

var oImage = new Image;
oImage.onload = myImageHasLoaded;
oImage.onerror = myImageHasFailed;
oImage.source = "myImageURL.jpg";
oImage.src = oImage.source;
Copy linkTweet thisAlerts:
@CazauthorAug 13.2003 — Whoohoo this could just make it, i'll test that, thanks! ?

With these events, i'll be able to monitor what's going wrong, thank you!

Caz
×

Success!

Help @Caz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...