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.
js applications. A fallback function specifies an action that is performed whenever a call to a remote service fails. For example, you can use a fallback function to send a customized message about service failures to the client.
By adding a fallback image, you can set a branded image to be used when no post thumbnail is found. This allows you to make sure that all your articles have a post thumbnail.
We can use the onerror attribute on the image to set a fallback. We use the onerror to set the error to null and set the src of the image to a fallback.
You can add an onerror
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 webpage doesn't crash if oops.gif
can't be loaded for some reason.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With