Refresh image with a new one at the same url

后端 未结 19 3542
南旧
南旧 2020-11-21 22:00

I am accessing a link on my site that will provide a new image each time it is accessed.

The issue I am running into is that if I try to load the image in the backgr

19条回答
  •  野的像风
    2020-11-21 22:35

    What I ended up doing was having the server map any request for an image at that directory to the source that I was trying to update. I then had my timer append a number onto the end of the name so the DOM would see it as a new image and load it.

    E.g.

    http://localhost/image.jpg
    //and
    http://localhost/image01.jpg
    

    will request the same image generation code but it will look like different images to the browser.

    var newImage = new Image();
    newImage.src = "http://localhost/image.jpg";
    var count = 0;
    function updateImage()
    {
        if(newImage.complete) {
            document.getElementById("theText").src = newImage.src;
            newImage = new Image();
            newImage.src = "http://localhost/image/id/image" + count++ + ".jpg";
        }
        setTimeout(updateImage, 1000);
    }
    

提交回复
热议问题