Hmm, that code looks very ragged to say the least. Even if it worked, the interval is every 5 seconds, much too frequently for an image that you say updates only every two minutes. In my experience, an interval half that of the event one is tracking is sufficient, so in this case, one minute, not 5 seconds would be good, but can only guarantee an update every three minutes with perhaps a one minute lag (most likely it will be more frequent). It depends upon how synced up you want to be. we could probably fine tune it to two minutes if there were a way of knowing when each two minute interval started. But, since we would be talking server versus client time, it might not be worth the attempt. Anyways, there are other problems with the code, the function is defined after the interval is set - might not matter. And there's no attempt to bust the natural caching many browsers will do that will frustrate loading a new image of the same name. IE is particularly bad in this regard. In any case, this is working here for me, give it a shot:
Code:
<html>
<body>
Test
</body>
<script>
var body = document.getElementsByTagName('body')[0];
body.style.backgroundImage = 'url(http://datilcam.com/cam/HCpic.jpg)';
function updateBg(){
var body = document.getElementsByTagName('body')[0];
body.style.backgroundImage = 'url(http://datilcam.com/cam/HCpic.jpg?cbust=' + new Date().getTime() + ')';
}
setInterval(function(){updateBg()}, 1000 * 60);
</script>
</html>
Maybe, for less lag, a half minute:
Code:
<html>
<body>
Test
</body>
<script>
var body = document.getElementsByTagName('body')[0];
body.style.backgroundImage = 'url(http://datilcam.com/cam/HCpic.jpg)';
function updateBg(){
var body = document.getElementsByTagName('body')[0];
body.style.backgroundImage = 'url(http://datilcam.com/cam/HCpic.jpg?cbust=' + new Date().getTime() + ')';
}
setInterval(function(){updateBg()}, 1000 * 30);
</script>
</html>
Regardless, you are not going to sync up perfectly. The displayed time on the image will often be a little behind local time, but not far. The tradeoff is, each time you update, the browser repaints the background, which is kinda ugly. If it's not crucial that you be as up to date as possible, you might even want to increase the interval to - say 5 minutes (1000 * 60 * 5). That way, every 5 minutes it will retrieve the most recent web image. If you eliminate the time stamp from the image, no one will know.
Bookmarks