Re-loading JS / JQuery after 1 second

I have a query calling a bunch of such images:

<a href='www.domain1.com'><img src='../image/img1.png' onerror='imgError(this);'/></a>
<a href='www.domain2.com'><img src='../image/img2.png' onerror='imgError(this);'/></a>

The problem is that during the call some images (~ 20%) are not ready yet. They need another second.

So, in js or jquery, what I would like to do is the error of getting images that failed, wait 1 second, and then try loading these failed images again. If they fail in the second attempt - well, I'm good with that. But I am not doing it right ... Should I not call the timeout inside another method in js?

function imgError(image) {
    image.onerror = "";
    image.src = image;

    setTimeout(function () {
        return true;
    }, 1000);
}
+4
source share
1 answer

Add a cache breaker.

function imgError(image) {
    image.onerror = null;
    setTimeout(function (){
        image.src += '?' + +new Date;
     }, 1000);
}

(, URL- , , , .)

+9

All Articles