Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with in line JS commands

    Hi

    I have a single webcam image that's refreshed every 5 secs with some js code which works fine. However I'm now attempting two images on the same page and sort of doubling up the code but changing the file names. Here's the code:-

    <div><img src="http://www.my_website/" width="450" height="350" alt="" name="dog1" ></div>
    <script language="JavaScript" type="text/javascript" >
    <!--
    var images = new Array();
    images[1] = "dogs.jpg";
    interval = 5000;

    function Refresh() {
    for (i=1; i<=1; i++) {
    tmp = new Date();
    tmp = "?" + tmp.getTime();
    document.images["cam" + i].src = images[i] + tmp;
    }
    setTimeout("Refresh()", interval);
    }

    Refresh();
    // -->
    </script>

    <div><img src="http://www.My_website/" width="375" height="300" alt="" name="cam0" ></div>
    <script language="JavaScript" type="text/javascript">
    <!--
    var tcimages = new Array();
    tcimages[0] = "cats.jpg";
    interval = 5000;

    function Refresh() {
    for (i=0; i<=0; i++) {
    tmp = new Date();
    tmp = "?" + tmp.getTime();
    document.images["cat" + i].src = tcimages[i] + tmp;
    }
    setTimeout("Refresh()", interval);
    }

    Refresh();
    // -->
    </script>

    My problem is that the result of using the code in this cofiguration results in the first image remaining static (not refreshing) and the second image refreshing OK. I can't seem to get both images refreshing so some help would be gratefully appreciated. Please keep it simple as I'm a js newbie.

    Many thanks

  • #2
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    your first images have name="dog1" but you refer to it by document.images["cam" + i].src where it should be document.images["dog" + i].src

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for your reply.

    I appologise for the typo's, I changed some of the actual names in the code and made some stupid mistakes. Below is the actual code for scrutiny, the problem is still apparent and any help would be appreciated....



    <div><img src="http://www.jacarandatree.co.uk/" width="450" height="350" alt="" name="uk1" ></div>
    <script language="JavaScript" type="text/javascript" >
    <!--
    var images = new Array();
    images[1] = "cornwall.jpg";
    interval = 5000;

    function Refresh() {
    for (i=1; i<=1; i++) {
    tmp = new Date();
    tmp = "?" + tmp.getTime();
    document.images["uk" + i].src = images[i] + tmp;
    }
    setTimeout("Refresh()", interval);
    }

    Refresh();
    // -->
    </script>

    <div><img src="http://www.jacarandatree.co.uk/" width="375" height="300" alt="" name="es0" ></div>
    <script language="JavaScript" type="text/javascript">
    <!--
    var tcimages = new Array();
    tcimages[0] = "tincam.jpg";
    interval = 5000;

    function Refresh() {
    for (i=0; i<=0; i++) {
    tmp = new Date();
    tmp = "?" + tmp.getTime();
    document.images["es" + i].src = tcimages[i] + tmp;
    }
    setTimeout("Refresh()", interval);
    }

    Refresh();
    // -->
    </script>

  • #4
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    You have 2 functions named Refresh.
    The result is that the second function overwrites the first.

    Try renaming one of the functions, together with the related initializing call.
    eg. to Refresh1

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    If the functions were defined using Refresh = function() { instead of function Refresh() { then they'd actually work independently with the second not overwriting the first until after the first had been run. Then they'd work as required.

    Defining functions with the word function first doesn't always work as expected (there are even places where you can't define functions that way) but the other way around with the word function after the = always works.

    A better solution to this particular situation would be to pass "uk" and "es" into the function as a parameter so that the function doesn't need to be duplicated at all.
    Last edited by felgall; 06-23-2012 at 10:44 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    I tried Felgall's option and this didn't work so I then tried Lerura's solution and this did, so problem solved.

    I will spend more time with Felgall's option to hopefully learn more.

    Many thanks to you both and the forum for this great service.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •