
Originally Posted by
Twey
The quotes you put around "beamer.src" mean you're literally assigning dog_array[dog_array.length] "beamer.src" instead of the actual source of the image.
That shouldn't actually matter. The array elements are evaluated using the eval global function, however that's just as bad.

Originally Posted by
yardbird
Problem is, every time the image changes, IE goes out to the net to re-get the image, even though all are preloaded.
I've encountered preloading weirdness with IE as well, but I'm a little fuzzy on the details at the moment. As I recall, if the preloading process was interrupted, IE would just stop trying, necessitating a network request next time around. I believe Firefox just keeps going. That may have no bearing on the problem at hand, though.
Can you provide a link to a test page so that we can see exactly what's going on?
face_num=parseInt(Math.random()*dog_array.length);
This is an extremely poor use of parseInt. The parseInt function works on strings, not numbers, so the expression would have to be evaluated, converted to a string, then parsed back to an integer. To convert the result of the expression to an integer, use Math.floor instead.
face_num=(isNaN(face_num))?0:face_num;
This is completely unnecessary. Both operands in the multiplication above are numbers, so the result is guaranteed to be a finite number - it will never be NaN.
document.images['slide'].src = eval(dog_array[face_num]);
The eval global function is unnecessary in probably 99% of all ECMAScript code, and is invariably a sign of poor quality or cluelessness on the part of the author. Harsh? Undoubtedly, but unfortunately true.
I'd write that code something like:
Code:
function isFunction(o) {return 'function' == typeof o;}
function isGenericObject(o) {return isObject(o) || isFunction(o);}
function isObject(o) {return !!o && ('object' == typeof o);}
Array.prototype.swap = function(i, j) {
var t = this[i]; this[i] = this[j]; this[j] = t;
};
Array.prototype.shuffle = function() {
var i = this.length; while(i--) {this.swap(i, Math.randomInt(i + 1));}
};
Math.randomInt = function(n) {
return Math.floor((Math.random() % 1) * n);
};
var faces = new (function(global) {
var dogs = ['images/faces/Darla100.jpg',
'images/faces/Beamer100.jpg',
'images/faces/Ebony100.jpg'],
current = 0,
interval = 2500,
length = dogs.length,
load, timer;
/* Check that necessary features
* are available before proceeding.
*/
if(!isGenericObject(global.Image) || !isGenericObject(global.setTimeout)
|| !isGenericObject(document.write)
|| !isGenericObject(document.images))
{return null;}
/* Randomise the order of the dogs array. */
dogs.shuffle();
document.write('<img alt="" src="' + dogs[0] + '" id="faces" name="faces"'
+' style="border-style: none; margin: 0 5px; vertical-align: top;">');
/* Prepare the preloading object. */
load = new Image();
function show() {
document.images.faces.src = dogs[current++];
/* If we've now moved through the entire set, */
if(length == current) {
/* reset the position, */
current = 0;
/* don't bother trying to preload any more, */
load = null;
/* and reshuffle the array (if you want to). */
/* dogs.shuffle(); */
}
if(load) {load.src = dogs[current];}
timer = setTimeout(show, interval);
};
(this.show = show).toString = function() {return 'faces.show();';};
})(this);
if(faces) {faces.show();}
Several hours of sleep later: I've altered the code above somewhat, mainly to accomodate IE because it has strange notions that functions are actually objects 
I don't know what sort of images you're using, so testing the preloading mechanism properly may not be useful. The current scheme tries to load the image that's next in line, rather than all of them at once. If the images are of a reasonable size, the current 2500ms interval should be enough. If not, that part may need to be altered.
Using the code is relatively simple: place everything except the last line where you want the img to appear. You can place the last line pretty much anywhere you want after that point.
To reiterate: this may not fix your problem with regard to caching.
Mike
Bookmarks