Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b2cab2e
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* imageCache.js - image caching framework. | ||
* Zoltan Hawryluk - http://www.useragentman.com/ | ||
* MIT License. | ||
*/ | ||
|
||
var imageCache = new function () { | ||
var me = this; | ||
|
||
var cache = []; | ||
var root = document.location.href.split('/'); | ||
|
||
root.pop(); | ||
root = root.join('/') + '/'; | ||
|
||
me.push = function (src, loadEvent) { | ||
|
||
if (!src.match(/^http/)) { | ||
src = root + src; | ||
} | ||
|
||
var item = new Image(); | ||
|
||
|
||
if (cache[src] && loadEvent) { | ||
loadEvent(src); | ||
} else { | ||
if (loadEvent) { | ||
item.onload = loadEvent; | ||
item.onerror = loadEvent; | ||
} | ||
cache[src]=item; | ||
} | ||
|
||
item.src = src; | ||
} | ||
|
||
me.pushArray = function (array, imageLoadEvent, imagesLoadEvent) { | ||
var numLoaded = 0; | ||
var arrayLength = array.length; | ||
for (var i=0; i<arrayLength; i++) { | ||
me.push(array[i], function (e) { | ||
if (imageLoadEvent) { | ||
imageLoadEvent(e); | ||
} | ||
numLoaded++; | ||
if (numLoaded == arrayLength) { | ||
setTimeout(function () { | ||
imagesLoadEvent(e); | ||
}, 1) | ||
|
||
} | ||
}) | ||
} | ||
} | ||
|
||
} |