This is my first time asking a question on this website which I have grown to love. I am trying to make games with JQuery and I need to preload sounds and images. If the images and sounds are already in the cache of the browser, it seems to work every time, but if they are not already in the cache of the browser, sometimes it freezes in the middle of loading, especially on Firefox. My question is, what is causing this to happen and how can I prevent it from happening? Refreshing the page usually solves the problem but I would like it to work flawlessly the first time if possible. Here's my code:
HTML:
<div id='loading_screen'>Loading <span id='how_much_loaded'>0</span>%</div>
<div id='content'>This is the content</div>
CSS:
#content{opacity:0}
#loading_screen{position:absolute;left:0px;top:0px}
Javascript:
var sounds=["sound0.wav","sound1.wav","sound2.wav","sound3.wav","sound4.wav","sound5.wav","sound6.wav","ding.wav"]
var images=["background-3_blue.jpg","background-3_green.jpg","background-3_yellow.jpg","background-3_red.jpg","play2train.png"]
var loaded_items=0;
var total_items=sounds.length+images.length;
function preload()
{
for (var index=0;index<images.length;index++)
{
$("#preload").append(
$("<img />").attr({
src: images[index],
onload: "counter();",
style: "display:none"})
)
}
for (var index=0;index<sounds.length;index++)
{
$("#preload").append(
$("<audio />").attr({
src: sounds[index],
oncanplaythrough='counter()',
style: "display:none"})
)
}
}
function counter()
{
loaded_items++;
$("#how_much_loaded").html((Math.round(loaded_items/total_items*100)))
if (loaded_items==total_items)
{
$("#loading_screen").remove();
$("#content").animate({"opacity":1});
}
}
Here's the game I'm working on. See if it works on your computer. Thanks!!