views:

127

answers:

4

I test the code in IE7, FF, Chrome, Safari and this problem occurs in Firefox only.

I have checked that the problem only occurs in FF 3.5.x, but not FF 3.0.x.

I want to make an image animation which has 10 images in total.

Currently, I use the following code to do it:

for (var i=1;i<=10;i++){
    img[i] = new Image();
    img[i].src = "images/survey/share_f"+i+".jpg"
}

var cornerno = 0;
function imganimate(){
     $("#surveyicon").attr("src",img[cornerno].src);
     //some logic to change the cornerno
     setTimeout("imganimate()",1000);
}

And then change an element's src to loop through the array "img".

however, firefox keeps requesting the images continuous (I expect it only requests each unique image just once).

What should I do?

A: 

You've already created ten DOM nodes, set their src and loaded the images. Why would you set the src again? You want to rotate those ten nodes in and out now. You can either toggle style.display or remove and insert the nodes.

Here's my suggestion. I'm not well versed in JQuery so there may be a few additional shortcuts I've overlooked:

var imgAmt = 10;
img = [];
for (var i=1;i<=imgAmt;i++){
    img[i] = document.createElement("img");
    img[i].src = "images/survey/share_f"+i+".jpg"
    img[i].style.display = "none";
    $("#surveyicon").appendChild(img[i]);
}
imganimate();

var cornerno = 0;
function imganimate(){
    cornerno++;
    cornerno = cornerno > imgAmt ? 1 : cornerno;
    for (var i=1;i<=imgAmt;i++){
     // hide all images but the index that matches cornerno:
     img[i].style.display = i==cornerno ? "" : "none";
    }
    setTimeout(imganimate,1000);
}
mwilcox
Can you post some sample code of this?
Billy
Nearly all the tutorials I find use similar code to do that.
Billy
I don't think it is good for me to use this code. I have done some logic on the #surveyicon node.I have just discovered that the problem occurs in FF 3.5.x only.
Billy
A: 

img is undefined. just add a line "var img = new Array();" before "for (var i=1;i<=10;i++){"

var img = new Array();

for (var i=1;i<=10;i++){
    img[i] = new Image();
    img[i].src = "images/survey/share_f"+i+".jpg";

}

var cornerno = 0;
function imganimate(){
    cornerno %= 10;
    cornerno++;
     $("#surveyicon").attr("src",img[cornerno].src);

     setTimeout("imganimate()",1000);
}

imganimate();
The code I post is just part of the code. The problem is for the strange behavior of firefox.
Billy
Also, it is better to use var img = [] to declare an array.
Billy
A: 

Try composing the images into a single image file like a sprite map and then use CSS positioning to shift the image around as a background image. It will be much faster and avoid any reloads.

Killroy
A: 

This seems a bug in FF3.5.x. Not sure whether the bug has already been fixed.

Billy