0

我正在上传图像,处理部分需要时间,我只能通过它来检测客户端。所以我使用这个脚本

function: loadImage(src, callback){
    var i = new Image(),        
    that = this;
    i.onload = function(){
        //we might get a 1 pixel image back from the webserver in case of a 404, which might also get cached
        if(i.width <= 1){
            setTimeout(function(){
                var newSrc = (src.indexOf('?') > -1? src.substring(0,src.indexOf('?')) : src) + '?rnd=' + new Date().getMilliseconds();
                i.src=null;
                i.src = newSrc;
            },5000);
            return;
        }
        callback(i);
    };
    //sometimes images are not there yet because they are being processed. So we have to rerun the function every x seconds
    i.onerror = setTimeout(function(){
        var newSrc = (src.indexOf('?') > -1? src.substring(0,src.indexOf('?')) : src) + '?rnd=' + new Date().getMilliseconds();
        i.src=null;
        i.src = newSrc;
    },5000);

    i.src = src;
}

因此,当图像返回 404 以防止丑陋的图像损坏时,许多网络服务器返回 1 像素图像,在这种情况下,我想再做一轮,直到我得到其他东西。

但是,当调用 onload 处理程序并重置 src 时,它不会导致新的图像加载。对此有何建议?

4

0 回答 0