2

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.