Skip to content

Instantly share code, notes, and snippets.

@zhanhongtao
Created April 28, 2015 12:24
Show Gist options
  • Save zhanhongtao/9b6b4ab548c6834e9ebb to your computer and use it in GitHub Desktop.
Save zhanhongtao/9b6b4ab548c6834e9ebb to your computer and use it in GitHub Desktop.
图片下载器, 支持 callback
;(function(root, factory) {
if(typeof define === 'function' && define.amd) {
define([], factory);
} else if(typeof exports === 'object') {
module.exports = factory();
} else {
root.imageLoader = factory();
}
})(this, function() {
'use strict';
var list = [];
var loading = {};
var cache = false;
function callback(ret, src) {
var cbs = loading[src];
if (cbs) {
for (var i = 0; i < cbs.length; ++i) {
var cb = cbs[i];
if (typeof cb === 'function') {
cb(ret);
}
}
delete loading[src];
if (cache && !ret) {
list.push([src, cbs]);
}
}
}
// 使用回调函数
// 以便支持 image 标签的 src 属性以及 background-image 样式
function load(src, cb, force) {
if (loading[src]) {
if (force) Array.prototype.push.apply(loading[src], cb);
return;
}
loading[src] = [cb];
var img = document.createElement('img');
img.onload = function() {
img.onerror = img.onload = null;
callback(true, src);
};
img.onerror = function() {
img.onerror = img.onload = null;
callback(false, src);
};
img.src = src;
}
// 手动调用. ex: offline -> online 时调用.
function reload() {
var item, stack = list.slice(0);
list.length = 0;
while(item = stack.pop()) {
load(item[0], item[1]);
}
}
return {
load: load,
reload: reload,
// Global setting
cache: function(use) {
cache = !!use;
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment