您可以使用onloadonerror事件来检测图片是否加载成功。根据加载结果来判断是否重新加载图片或保持原图片。

以下是一个可能的解决方案:

window.refreshDeviceInterval = setInterval(() => {
  const img = new Image();
  img.onload = function() {
    // 图片加载成功
    window.deviceImgUrl = this.src;
    $("#devicePreviewImg").attr("src", window.deviceImgUrl);
    window.deviceImgUrlBak = window.deviceImgUrl;
  };
  img.onerror = function() {
    // 图片加载失败
    // 保持原图片
    window.deviceImgUrl = window.deviceImgUrlBak;
  };
  img.src = getContext() + "/uploadPath/autoJsTools/" + this.deviceInfo.deviceUuid + "/tempImg.jpg" + "?t=" + new Date().getTime();
}, this.devicePreviewParam.clientSpace);

在这个解决方案中,我们创建了一个新的Image对象,并设置onloadonerror事件处理程序。当图片加载成功时,onload事件将被触发,将新的图片URL赋值给window.deviceImgUrl,然后更新#devicePreviewImg元素的src属性。当图片加载失败时,onerror事件将被触发,将保持原图片URL。

请注意,为了避免浏览器缓存问题,我们在图片URL后添加了一个随机参数?t=new Date().getTime()。这将强制浏览器每次都重新加载图片

windowrefreshDeviceInterval = setInterval = windowdeviceImgUrl = getContext + uploadPathautoJsTools + thisdeviceInfodeviceUuid + tempImgjpg + t= + new DategetTime;

原文地址: https://www.cveoy.top/t/topic/ieIv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录