[removed] how to force Image() not to use the browser cache?

前端 未结 4 1567
执念已碎
执念已碎 2020-12-16 18:52

If I load the nextimg URL manually in the browser, it gives a new picture every time I reload. But this bit of code shows the same image every iteration of draw()

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 19:33

    That actually sounds like a bug in the browser -- you could file at http://bugs.webkit.org if it's in Safari or https://bugzilla.mozilla.org/ for Firefox. Why do i say potential browser bug? Because the browser realises it should not be caching on reload, yet it does give you a cached copy of the image when you request it programmatically.

    That said are you sure you're actually drawing anything? the Canvas.drawImage API will not wait for an image to load, and is spec'd to not draw if the image has not completely loaded when you try to use it.

    A better practice is something like:

        var myimg = new Image();
        myimg.onload = function() {
            var rx=Math.floor(Math.random()*100)*10
            var ry=Math.floor(Math.random()*100)*10
            ctx.drawImage(myimg,rx,ry);
            window.setTimeout(draw,0);
        }
        myimg.src = 'http://ohm:8080/cgi-bin/nextimg'
    

    (You can also just pass draw as an argument to setTimeout rather than using a string, which will save reparsing and compiling the same string over and over again.)

提交回复
热议问题