img设置(retry记录重试次数):
<img id="img" src="flower.png" retry="0" >
js:
img加载错误事件中每隔1.5秒进行重试,如果重试4次后,设置默认图片
$('#img').error(function() { var _this=$(this); var retry=_this.attr("retry"); setTimeout(function () { if(retry>3){ _this.attr("src","/static/images/imgError.png");//设置碎图 }else{ retry++; _this.attr("retry", retry);//重试次数+1 _this.attr('src', _this.attr("src"));//继续刷新图片 } },1500) })
此方法也适用于移动端加载的H5页面。