Hi,
I've written a simple script to randomly swap the background image of an element on my page after a set number of seconds.
My problem is that although I am initiating the preload of the images within the script, the change function may be called before the images have finished preloading.
Forgive the code if it's a bit lumpy, I've not done much Javascript for a very long time! Just getting back into it.
here are my basic functions (from an external file)
And I have this in the page itself
The addLoadEvent function is a tried and tested thing that simply adds a function to a list that gets executed once the page is loaded. It's so I can have multiple things going off for the onload event.
The process goes as follows:
the body.onload event triggers preloadImages(), which takes the array defined in the page itself and begins preloading the images.
How can I make the script wait until the images are fully loaded before it returns from that preload function?
<honk>*:O)</honk>
Foamcow Heavy Industries - Web site design in Cheltenham and Gloucester
Ham and Jam - British & Commonwealth forces mod for Half Life 2
I've written a simple script to randomly swap the background image of an element on my page after a set number of seconds.
My problem is that although I am initiating the preload of the images within the script, the change function may be called before the images have finished preloading.
Forgive the code if it's a bit lumpy, I've not done much Javascript for a very long time! Just getting back into it.
here are my basic functions (from an external file)
Code:
function startTimer(secs) {
setTimeout("randomBGchange()",secs*1000);
}
function preloadImages(){
//array of background images
var preload_image_object = new Image();
//preload the images stored in BGimages array - defined on page
var i = 0;
for(i=0; i<=BGimages.length; i++) {
preload_image_object.src = BGimages[i];
}
startTimer(10);
return true;
}
function randomBGchange() {
//pick a random image
newBGimage=parseInt(Math.random()*BGimages.length);
//element we wish to change the background of
var content = document.getElementById('content');
//change the background
content.style.backgroundImage="url("+BGimages[newBGimage]+")";
startTimer(3);
}
And I have this in the page itself
Code:
<script type="text/javascript" charset="utf-8">
var BGimages = new Array();
BGimages[BGimages.length] = "images/homepage/area51.jpg";
//BGimages[BGimages.length] = "images/base/sshw_Bisque-Hi-res.jpg";
BGimages[BGimages.length] = "images/homepage/realItBG.jpg";
BGimages[BGimages.length] = "images/homepage/innopackBG.jpg";
BGimages[BGimages.length] = "images/homepage/clockBG.jpg";
addLoadEvent(preloadImages);
</script>
The addLoadEvent function is a tried and tested thing that simply adds a function to a list that gets executed once the page is loaded. It's so I can have multiple things going off for the onload event.
The process goes as follows:
the body.onload event triggers preloadImages(), which takes the array defined in the page itself and begins preloading the images.
How can I make the script wait until the images are fully loaded before it returns from that preload function?
<honk>*:O)</honk>
Foamcow Heavy Industries - Web site design in Cheltenham and Gloucester
Ham and Jam - British & Commonwealth forces mod for Half Life 2