How To Refresh One Image?

Discussion in 'JavaScript' started by James12513, Oct 11, 2007.

  1. #1
    Could some one please tell me how I would refresh one image every time 2 minutes?

    Can some please show me a source code, I've been trying for ages. :)
     
    James12513, Oct 11, 2007 IP
  2. KatieK

    KatieK Active Member

    Messages:
    116
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Hmmm... Will the image be the same file name? (And it's the server that changes the image?)
     
    KatieK, Oct 11, 2007 IP
  3. James12513

    James12513 Well-Known Member

    Messages:
    1,149
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    138
    #3
    Yes the image will be the same file name and the server will be the one changing the image.
     
    James12513, Oct 11, 2007 IP
  4. le_gber

    le_gber Peon

    Messages:
    28
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If it's the server that changes the images you cannot use javascript for that. You have to use a sheduled task or cron job.

    Why not use javascript instead. Name your images image1.gif, image2.gif etc... and do something like

    set timer;
    count to 20 seconds;
    if i < max image number
    img.src = image.gif

    add 1 to i​
    else
    image src = image.gif​
    start over

    or just create an animated gif that changes every 20 seconds
     
    le_gber, Oct 11, 2007 IP
    James12513 likes this.
  5. KatieK

    KatieK Active Member

    Messages:
    116
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    58
    #5
    This will do it. Just change both instances of "image.png" to whatever your image name will be.

    First I set up a function that uses the DOM to specify the image. (The image would change onscreen if it were a different filename in the JS function.)

    Then, in the body of the document, I set up a counter that calls the function every 180000 milliseconds, or every 2 minutes.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Test</title>
    <script type="text/javascript">
    function imgRefresh()
      {	document.getElementById("changeMe").src = "image.png"; }
    </script>
    </head>
    <body>
    <img src="image.png" id="changeMe" />
    <script type="text/javascript">
      setInterval('imgRefresh()', 180000);
    </script>
    </body>
    </html>
    
    Code (markup):
     
    KatieK, Oct 11, 2007 IP
    James12513 likes this.
  6. James12513

    James12513 Well-Known Member

    Messages:
    1,149
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    138
    #6
    Thank you le_gber and KatieK, I'm going to try out those methods now. :)

    I'm not entirely sure my server changes the images, I don't think so.
     
    James12513, Oct 12, 2007 IP