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

前端 未结 2 1028
抹茶落季
抹茶落季 2020-12-21 02:59

I am running a online photography portfolio and sometimes, 1 or 2 images on a page fails to load. and refreshing will display the failed to load image.

Scenario: I c

2条回答
  •  粉色の甜心
    2020-12-21 03:10

    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);  
    });
    

提交回复
热议问题