The problem you are facing is mostly due to the fact that the URL really does not change. This may:
- Not even trigger a new request from your img tag
- Be served from your browser cache
So odds are, your image does not change.
One way to fix this would be to pass an additional dummy query parameter which changes on each request.
Sample URL:
https://picsum.photos/200/300/?random&dummyParam=1
You can increment dummyParam each time so it looks like a new URL to the img tag and the browser.
Sample Code:
var cb = 0;
setInterval(function() {
document.getElementsByTagName("body")[0].style.backgroundImage = "url(https://picsum.photos/200/300/?random&cb=" + (++cb) + ")";
}, 1000)
img {
width: 200px;
height: auto;
}
EDIT:
@mplungjan's answer uses milliseconds since 1970 as the random dummy parameter and this may be better as you don't have to have a separate variable to track the counter.
Sample code:
document.getElementsByTagName("body")[0].style.backgroundImage = "url(https://picsum.photos/200/300/?random&cb=" + (+new Date()) + ")";