-->

Fallback image and timeout - External Content. Jav

2019-01-21 23:10发布

问题:

What is the best way to set up a local fallback image if the external image does not load or takes too long to load.

回答1:

You can add an oneror handler:

<img src="http://example.com/somejpg.jpg"
    onerror='this.onerror = null; this.src="./oops.gif"' />

Note setting onerror to null in the handler so that the browser doesn't die if oops.gif can't be loaded for some reason.



回答2:

Try to make use of the Image.complete property.

var img = new Image(w,h)
img.src = "http://...";

Now check periodically if img.complete is true and call some fallback mechanism shuold it still be false after n seconds.