JS图片加载异常、超时报错问题如何解决
Admin 2022-11-02 群英技术资讯 1145 次浏览
直接在img标签上使用内联事件处理图片加载失败的情况,但是这种方式代码侵入性太大,指不定就会有地方漏掉。
<img src='xxxxx' onerror="this.src = 'default.png'">
第一个方案侵入性太大,我们将入口收敛,为所有img标签统一添加error处理事件。
const imgs = document.getElementsByTagName('img') Array.prototype.forEach.call(imgs, img => { img.addEventListener('error', e => { e.target.src = 'default.png' }) })
为每个img添加事件处理函数的代价还是高了点,我们知道一般事件会经历三个阶段:
根据MDN文档中的描述:
When a resource (such as an <img> or <script>) fails to load, an error event using interface Event is fired at the element that initiated the load, and the onerror() handler on the element is invoked. These error events do not bubble up to window, but can be handled with a EventTarget.addEventListener configured with useCapture set to true.
我们可以知道img和srcipt标签的error并不会冒泡,但是会经历捕获阶段和处于目标阶段。前两个方案就是利用处于目标阶段触发事件函数,这一次我们在捕获阶段截获并触发函数,从而减少性能损耗。
document.addEventListener( 'error', e => { let target = e.target const tagName = target.tagName || '' if (tagName.toLowerCase = 'img') { target.src = 'default.png' } target = null }, true )
上面的方案有两个缺点:
所以我们可以为每个img标签额外添加一个data-retry-times计数属性,当重试超过限制次数后就用base64图片作为默认兜底。
document.addEventListener( 'error', e => { let target = e.target const tagName = target.tagName || '' const curTimes = Number(target.dataset.retryTimes) || 0 if (tagName.toLowerCase() === 'img') { if (curTimes >= 3) { target.src = 'data:image/png;base64,xxxxxx' } else { target.dataset.retryTimes = curTimes + 1 target.src = target.src } } target = null }, true )
上面方式是采用替换src的方式来展示兜底图,这种解决方式有一个缺陷:
所以还有一种更好的方式,就是利用CSS伪元素::before和::after覆盖原本元素,直接展示兜底base64图片。
CSS样式如下:
img.error { display: inline-block; transform: scale(1); content: ''; color: transparent; } img.error::before { content: ''; position: absolute; left: 0; top: 0; width: 100%; height: 100%; background: #f5f5f5 url(data:image/png;base64,xxxxxx) no-repeat center / 50% 50%; } img.error::after { content: attr(alt); position: absolute; left: 0; bottom: 0; width: 100%; line-height: 2; background-color: rgba(0,0,0,.5); color: white; font-size: 12px; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
JS代码如下:
document.addEventListener( 'error', e => { let target = e.target const tagName = target.tagName || '' const curTimes = Number(target.dataset.retryTimes) || 0 if (tagName.toLowerCase() === 'img') { if (curTimes >= 3) { target.classList.remove('error') target.classList.add('error') } else { target.dataset.retryTimes = curTimes + 1 target.src = target.src } } target = null }, true )
目前大多数应用都会接入CDN来加速资源请求,但是CDN存在节点覆盖不全的问题,导致DNS查询超时,此时如果切换Domain可能就会加载成功。
我们可以使用嗅探的方式,测试CDN提供的Domain是否能够正常访问,如果不行或者超时就及时切换成可访问Domain。 其中有几个注意点:
// 防止嗅探图片存在缓存,添加时间戳保持新鲜度 export const imgUri = `/img/xxxxx?timestamp=${Date.now()}${Math.random()}`; export const originDomain = 'https://sf6-xxxx.xxxx.com' // 可采用配置下发的方式 export const cdnDomains = [ 'https://sf1-xxxx.xxxx.com', 'https://sf3-xxxx.xxxx.com', 'https://sf9-xxxx.xxxx.com', ]; export const validateImageUrl = (url: string) => { return new Promise<string>((resolve, reject) => { const img = new Image(); img.onload = () => { resolve(url); }; img.onerror = (e: string | Event) => { reject(e); }; // promise的状态不可变性,使用setTimeout模拟超时 const timer = setTimeout(() => { clearTimeout(timer); reject(new Error('Image Load Timeout')); }, 10000); img.src = url; }); }; export const setCDNDomain = () => { const cdnLoop = () => { return Promise.race( cdnDomains.map((domain: string) => validateImageUrl(domain + imgUri)), ).then(url => { window.shouldReplaceDomain = true; const urlHost = url.split('/')[2]; window.replaceDomain = urlHost; }); }; return validateImageUrl(`${originDomain}${imgUri}`) .then(() => { window.shouldReplaceDomain = false; window.replaceDomain = ''; }) .catch(() => { return cdnLoop(); }); }; // 替换URL export const replaceImgDomain = (src: string) => { if (src && window.shouldReplaceDomain && window.replaceDomain) { return src.replace(originDomain.split('/')[2], window.replaceDomain); } return src; };
该方案需要后台同学配合,由后台判断当前当前可用Domain并返回。
getUsefulDomain() .then(e => { window.imgDomain = e.data.imgDomain || '' })
实际应用中背景图也会加载失败,通常这些元素没有error事件,所以也就无从捕获error事件了。此时就可以利用dispatchEvent,它同样拥有捕获阶段,MDN文档上是这么介绍的:
Dispatches an Event at the specified EventTarget, (synchronously) invoking the affected EventListeners in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually with dispatchEvent().
可以看到支持度也还是可以的,我们首先需要自定义一个事件并初始化这个事件,在背景图加载失败的时候触发这个自定义事件,最后在上层捕获这个事件并执行事件函数。
自定义事件有两种方式:
这里以第二种为例,根据MDN文档的用法创建一个自定义事件:
const event = new Event('bgImgError')
使用前面定义的方法嗅探图片资源的情况。
validateImageUrl('xxx.png') .catch(e => { let ele = document.getElementById('bg-img') if (ele) { ele.dispatchEvent('bgImgError') } ele = null })
document.addEventListener( 'bgImgError', e => { e.target.style.backgroundImage = "url(data:image/png;base64,xxxxxx)" }, true )
到此,关于“JS图片加载异常、超时报错问题如何解决”的学习就结束了,希望能够解决大家的疑惑,另外大家动手实践也很重要,对大家加深理解和学习很有帮助。如果想要学习更多的相关知识,欢迎关注群英网络,小编每天都会给大家分享实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
时钟是我们查看时间的一种工具,很多人家家里都会摆放一个时钟,那么我们用代码能实现一个时钟效果吗?答案是可以用的,这篇文章主要教大家用JS代码配置CSS样式来实现一个动态的时钟效果,效果如下图所示,下面我们一起来学习下。
小程序实现日历打卡功能 本文实例为大家分享了小程序实现日历打卡功能的具体代码,供大家参考,具体内容如下 一.效果图展示 老惯例,先上效果图 二.实现思路 1.日历展示 例如下图中: 2021月7月打卡日历页面,共35个日期数据,上月残余4天+本月31天: 2021月6月打卡日历页面,共35个日期数据,上月残余2天+本月30天+下月残余3天: 2021月5月打卡日历页面,共42个日期数据,上月残余6天+本月31天+下月残余5天. [结论]打卡日历页面存在展示35个或42个日期数据的情况,35个或42个日期数据=当前显示月
本文我们来了解CocosCreator对象池的使用,对于CocosCreator对象池的使用操作具体有六个步骤,下文都有详细的介绍,感兴趣的朋友接下来就跟随小编来了解看看吧。
本篇文章给大家带来了关于javascript的相关知识,主要介绍了JavaScript中异步与回调的基本概念,以及回调地狱现象,本文主要介绍了异步和回调的基本概念,二者是JavaScript的核心内容,下面一起来看依稀啊,希望对大家有帮助。
这篇文章主要为大家介绍了JS继承与工厂构造及原型设计模式详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008