Try this out:
Code:
<html>
<head>
<script type="text/javascript">
var loadedImageList = [];
var imagePath = "images/random185x185/";
if (!Array.indexOf) {
Array.prototype.indexOf = function(searchFor) {
var item;
for (var i=0; item=this[i]; i++) {
if (item == searchFor) {
return i;
}
}
return -1;
};
}
function Flipper(target, srcList, delay) {
// multi-compatible delay image swapper dealie
this.target = target;
this.srcList = srcList;
this.delay = delay;
this.current = 0;
doPreload(srcList);
doSetTimeout(this);
}
function doSetTimeout(obj) {
window.setTimeout(function () {switcher(obj)}, obj.delay);
}
function switcher (obj) {
obj.target.setAttribute("src", imagePath + obj.srcList[obj.current++]);
if (obj.current > obj.srcList.length - 1) {
obj.current = 0;
}
doSetTimeout(obj);
}
function doPreload(list) {
var imgFile;
for (var i=0; imgFile = list[i]; i++) {
if (loadedImageList.indexOf(imgFile) == -1) {
var img = new Image();
img.src = imagePath + imgFile;
loadedImageList.push(imgFile);
}
}
}
</script>
</head>
<body>
<img id="img1" src="something.jpg" /><img id="img2" src="something.jpg" /><img id="img3" src="something.jpg" />
<script type="text/javascript">
new Flipper(document.getElementById("img1"), ["cyan.jpg","magenta.jpg"], 500);
new Flipper(document.getElementById("img2"), ["orange.jpg","yellow.jpg","blue.jpg"], 2000);
new Flipper(document.getElementById("img3"), ["cyan.jpg","blue.jpg","green.jpg","magenta.jpg","orange.jpg","yellow.jpg"], 5000);
</script>
</body>
</html>
Bookmarks