Refresh image with a new one at the same url

后端 未结 19 3304
南旧
南旧 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:46

    After creating the new image, are you removing the old image from the DOM and replacing it with the new one?

    You could be grabbing new images every updateImage call, but not adding them to the page.

    There are a number of ways to do it. Something like this would work.

    function updateImage()
    {
        var image = document.getElementById("theText");
        if(image.complete) {
            var new_image = new Image();
            //set up the new image
            new_image.id = "theText";
            new_image.src = image.src;           
            // insert new image and remove old
            image.parentNode.insertBefore(new_image,image);
            image.parentNode.removeChild(image);
        }
    
        setTimeout(updateImage, 1000);
    }
    

    After getting that working, if there are still problems it is probably a caching issue like the other answers talk about.

提交回复
热议问题