Make a browser pool for the image until it is available

I have a small http server that generates some images on the fly, and the generation process may take some time. After generation, the image is cached indefinitely.

Currently, if a user requests an image that is not cached, the server returns 202 Acceptedwith a header Refresh. If the image is cached, sent 301 Permanently Moved, and the user is redirected to a unique URL (different images can have the same unique URL).

The whole system is interrupted if the image refers to a tag <img>(at least Firefox). Can this be solved without Javascript? If not, what does the script look like?

+5
source share
1 answer

I'm not sure you can do this without Javascript, but could you do this with ajax? I mean, I’ll point to the server, and then just check if it exists ... then if it displays, try again after 30 seconds, it could be something like:

function getImage(img) {
 $.ajax({
            cache: true,
            url: <<ADDRESS>>,
            data: "",
            timeout: 60,
            error: function (jqXHR, error, errorThrown) {
                setTimeout(function() {
                    getImage(img);
                }, 30000);
            },
            success: function (data) {
                //set the image
            }
        });
}

Well, then you hope that the image goes down at some point.

The only other option would be to generate an image before its request? For example, if he simply creates a thumbnail for a photo gallery, why wait until she is asked to generate? Just generate it as soon as you have it?

Hope this helps / makes sense.

+2
source

All Articles