Refresh image with a new one at the same url

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

    Here's my solution. It's very simple. The frame scheduling could be better.

    <!doctype html>
    <html>
        <head>
            <meta charset="utf-8">      
            <title>Image Refresh</title>
        </head>
    
        <body>
    
        <!-- Get the initial image. -->
        <img id="frame" src="frame.jpg">
    
        <script>        
            // Use an off-screen image to load the next frame.
            var img = new Image();
    
            // When it is loaded...
            img.addEventListener("load", function() {
    
                // Set the on-screen image to the same source. This should be instant because
                // it is already loaded.
                document.getElementById("frame").src = img.src;
    
                // Schedule loading the next frame.
                setTimeout(function() {
                    img.src = "frame.jpg?" + (new Date).getTime();
                }, 1000/15); // 15 FPS (more or less)
            })
    
            // Start the loading process.
            img.src = "frame.jpg?" + (new Date).getTime();
        </script>
        </body>
    </html>
    
    0 讨论(0)
  • 2020-11-21 22:41

    No need for new Date().getTime() shenanigans. You can trick the browser by having an invisible dummy image and using jQuery .load(), then creating a new image each time:

    <img src="" id="dummy", style="display:none;" />  <!-- dummy img -->
    <div id="pic"></div>
    
    <script type="text/javascript">
      var url = whatever;
      // You can repeat the following as often as you like with the same url
      $("#dummy").load(url);
      var image = new Image();
      image.src = url;
      $("#pic").html("").append(image);
    </script>
    
    0 讨论(0)
  • 2020-11-21 22:42

    One answer is to hackishly add some get query parameter like has been suggested.

    A better answer is to emit a couple of extra options in your HTTP header.

    Pragma: no-cache
    Expires: Fri, 30 Oct 1998 14:19:41 GMT
    Cache-Control: no-cache, must-revalidate
    

    By providing a date in the past, it won't be cached by the browser. Cache-Control was added in HTTP/1.1 and the must-revalidate tag indicates that proxies should never serve up an old image even under extenuating circumstances, and the Pragma: no-cache isn't really necessary for current modern browsers/caches but may help with some crufty broken old implementations.

    0 讨论(0)
  • 2020-11-21 22:43

    I solved this problem by sending the data back through a servlet.

    response.setContentType("image/png");
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache, must-revalidate");
    response.setDateHeader("Expires", 0);
    
    BufferedImage img = ImageIO.read(new File(imageFileName));
    
    ImageIO.write(img, "png", response.getOutputStream());
    

    Then from the page you just give it the servlet with some params to grab the correct image file.

    <img src="YourServlet?imageFileName=imageNum1">
    
    0 讨论(0)
  • 2020-11-21 22:44

    Try using a worthless querystring to make it a unique url:

    function updateImage()
    {
        if(newImage.complete) {
            document.getElementById("theText").src = newImage.src;
            newImage = new Image();
            number++;
            newImage.src = "http://localhost/image.jpg?" + new Date();
        }
    
        setTimeout(updateImage, 1000);
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题