Is it possible to reload a specific image if it did not finish loading after a preset amount of time? javascript/jquery

核能气质少年 提交于 2019-11-29 15:30:13

@Pointy and @Gaby are right in their comments. Still I was curious about how to accomplish this.

This is what I came up with for what it's worth. Untested, though.

var images = $('img');  // Get all images. (you'll want to modify the selector
                        //    to work with only the desired images)

images.load(function() {       // Add a load event hander that removes 
    images = images.not(this); //    each image from images once loaded.
});

setTimeout(function(){        // After 10 seconds, iterate over each remaining
    images.each(function() {  //     image, reloading each one
        // reload this image
    });
},10000);  // run after 10 seconds

Put below code at end of page:

$('img').error(function(){
  var src= $(this).attr('src');
  if (window.console) {
       console.log('reload image '+ src);
  }
  var i= src.indexOf("&random=");
  if(i > -1) {
     src= src.substring(0,i);
  }
  i = src.indexOf("?random=");
  if(i > -1) {
     src= src.substring(0,i);
  }
  if(src.indexOf('?') > -1 ) {
      src= src+"&random="+ (new Date().getTime());
  } else  {
      src= src+"?random="+ (new Date().getTime());
  }
  $(this).attr('src', src);  
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!