Periodically update images on the page

I create a page to display a bunch of webcam images and periodically update them so that the page can be used for instant monitoring. However, I am having problems with periodic reboots. My code looks something like this:

<div class='cameras'> 
    <div class='camera'> 
      <h4>Desk</h4> 
      <img height='240' src='http://somehost/cameras/cam0/lastsnap.jpg' width='320'> 
    </div> 
    <div class='camera'> 
      <h4>Main Room</h4> 
      <img height='240' src='http://somehost/cameras/cam1/lastsnap.jpg' width='320'> 
    </div> 
    <div class='camera'> 
      <h4>Studio</h4> 
      <img height='240' src='http://somehost/cameras/cam2/lastsnap.jpg' width='320'> 
    </div> 
  </div> 

Ideally, I would like these things to reboot every couple of seconds from their specified URLs without the need to generate separate JS for each camera. I have jQuery for use in several other pieces, so sticking with that would be great - again, a simple JS solution is fine too.

Any ideas StackOverflow JS Gods?

+3
source share
4

, :

function refreshCameras() {
  $('.camera img').attr('src', function(i, old) { return old.replace(/\?.+/,"?i=" + (Math.random()*1000)); });
  setTimeout(refreshCameras, 1000);
}
function refreshCamerasFirst() {
  $('.camera img').attr('src', function(i, old) { return old + "?i=" + (Math.random()*1000); });
  setTimeout(refreshCameras, 1000);
}
$(function() {
    setTimeout(refreshCamerasFirst, 1000);
});

img "" , URL-, , URL- , .

+7

, html

<meta http-equiv="refresh" content="1">

- .

http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm

+1

[ ]

var img = []; //just an image source. you can write your own code for image source

img[0] ='http://site.com/pics/pic.jpg';
img[1] ='http://site.com/pics/pic1.jpg';
img[2] ='http://site.com/pics/pic2.jpg';
img[3] ='http://site.com/pics/pic3.jpg';

$(function() {
    $.each(img, function(i, val) {
        var images = new Image();
        images.src = val;    //preloading images for my example purpose
    });
    function reload() {
        $('img.alter').each(function() { //generate a url for  image source. 
            var src = img[Math.floor(Math.random() * img.length)];
            $(this).attr('src',src);
        });
    }   
    setInterval(reload , 5000)
});

PS:

+1

<meta http-equiv="Refresh" content="2; URL=yourpage.php">

It works with cool.Checkout text with images

0
source

Source: https://habr.com/ru/post/1768812/


All Articles