Reputation: 55
Is there any way in JavaScript alone you can wait until an image file exists on the server?
For a better explanation of my current situation users can press a button to get an image rendered but as of now they have to refresh for it to load. I wanted a better way to do this by deleting the previous image and then waited until a new one existed on the server which at that point it would show the new Image.
Upvotes: 2
Views: 257
Reputation: 163232
Assuming you don't have any way for the server to notify the client that the image is ready, you could poll for the image with a simple fetch.
fetch('https://example.com/some-image.jpg', {
method: 'HEAD'
}).then((res) => {
if (res.ok) {
// Your image is ready
}
});
Do that every 5 or 10 seconds, or whatever is appropriate for your use case.
Upvotes: 2