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 10 of 10

Thread: setInterval()

  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    setInterval()

    Hey all,

    I'm trying to use setInterval() to refresh an image continuously. I opted to use setInterval() outside the function it calls versus using setTimeout() in the function because I do not want the function execution time to effect the refresh rate to dramatically.

    Either way, my test page is very basic:

    Code:
    <html>
    <head>
    </head>
    <body>
    
    <a href="#webprocesses"><font color="darkgreen" size=+1><b>Web Procs</b></font></a>&nbsp;&nbsp;&nbsp;
    
    <table align="center">
    <tr>
    <td valign="top"><a name="webprocesses"></a></td>
    <td>
    <table>
    <tr><td align="center"><font color="darkred">Web Server Processes</font></td></tr>
    <tr><td><a href="webprocgraph.html"><img name="WsP"></a></td></tr>
    <tr><td><br></td></tr>
    </table>
    </td>
    </tr>
    </table>
    <SCRIPT language="JavaScript" type="text/javascript">
    
    Start();
    setInterval("Start()", 10000);
    function Start()
    {
        document.images.WsP.src='/cgi-bin/GraphingPortal.cgi?file=webprocesses2H.xml';
    }
    </SCRIPT>
    <body>
    </html>
    I've researched many sites and these forums. I'm almost positive that the syntax is correct. For some reason, it will only reload once, then it just stops doing it. For the heck of it I tried using setTimeout() in Start() and had the same results. I get the initial load, but thats it. I know the script has to be located at the bottom as the image object has to be defined before I can use document.image.

    Anyone have any ideas what might cause this?

    Matt

    *EDIT* I just found it works fine in IE6. It is not working in Firefox. Unfortunatly this will be used in a predominantly Firefox shop.

    *EDIT #2* So upon further testing I found something very strange. I open the page in Firefox and it does not work. I open a second instance of the page in a new firefox window or tab and bingo, it works like a champ, as does the original instance still open. I close one and the other stops working. How the heck does this happen?!
    Last edited by windago; 08-09-2006 at 04:57 PM.

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kentucky
    Posts
    133
    Thanks
    0
    Thanked 1 Time in 1 Post
    I'd say it's most likely due to the name of your function: Start();

    That's most likely a reserved word, so you're confusing the browser. Try renaming the function to something unique, like startImageRefresh().

    Let me know what you find out.

  • #3
    New Coder
    Join Date
    Jun 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I gave that a shot. Didn't work unfortuantly.

    Would DOM scripting have any impact at all? I'm not to familiar with it but someone mentioned the idea to me.

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kentucky
    Posts
    133
    Thanks
    0
    Thanked 1 Time in 1 Post
    I've been testing your code, and I can't find anything wrong with it. Maybe your problem is with the '/cgi-bin/GraphingPortal.cgi?file=webprocesses2H.xml' source.

  • #5
    New Coder
    Join Date
    Jun 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for checking for me. I'm positive it's not a problem with the file linked. That is just a camgraphing utility. To test I linked to just a standard image, and the problem remained the same. IE6 worked. 1 instance of firefox, not dice. 2 instances and bingo it works.

    *EDIT* I also was able to recreate the problem on a number of different machines here...

  • #6
    Regular Coder RexxCrow's Avatar
    Join Date
    Jul 2006
    Location
    California
    Posts
    275
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Try it this way... BTW thanks, ur code gave me an idea to try for a prob I was having, I tried it and now it works perfecto'!:

    Code:
    Start();
    function Start(){
    document.images.WsP.src='/cgi-bin/GraphingPortal.cgi?file=webprocesses2H.xml';
    setInterval("Start()",10000);
    }
    
    ...or maybe even?
    
    Start();
    function Start(){
    document.images.WsP.src='/cgi-bin/GraphingPortal.cgi?file=webprocesses2H.xml';
    }
    setInterval("Start()",10000);

  • #7
    New Coder
    Join Date
    Jun 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The first method would be in bad form, as setInterval() basically creates a non-ending loop. Would not want to call that over and over. It that configuration I had tied using setTimeout() with the same results.

    The second option you listed I have tried with the same results. I'm certain for all intents and purposes, the code is exactly as should be. I did forget to mention I'm running Fedora Core 5 and using Firefox. My pc is not a windows machine. I'm not sure if that would impact anything...

  • #8
    Regular Coder RexxCrow's Avatar
    Join Date
    Jul 2006
    Location
    California
    Posts
    275
    Thanks
    6
    Thanked 2 Times in 2 Posts
    I am calling it from within another function and it works find. as below:


    Code:
    window.onload=function(){matrix();}
    function matrix(){
    ...
      reloaded();
     }
    function reloaded(){
     setInterval("matrix()",45000);
     }
    
    So possibly more like:
    
    window.onload=function(){update();}
    function update(){
    document.images.WsP.src='/cgi-bin/GraphingPortal.cgi?file=webprocesses2H.xml';
    start();
    }
    function start(){
    setInterval("update()",10000);
    }

    Maybe that capital S is causing an issue and am not sure, though just more of wondering what about using " " rather then ' ' ?

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Maks sure that you only call the setInterval once. If you include it in the loop then it will start a geometric progression and by the end of a minute it will be running 32 instances of the code, by the end of two minutes it will be running over 2000 instances of the code which will almost certainly bring the browser to a complete halt within a few minutes.
    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.

  • #10
    Regular Coder RexxCrow's Avatar
    Join Date
    Jul 2006
    Location
    California
    Posts
    275
    Thanks
    6
    Thanked 2 Times in 2 Posts
    With the way that I have it now I have let it run for several hours and have had not visible problems, I just tested it again with an alert inside the setIntrval function and the box only displayed upon the initial loading of the page and then never again in 1/2 dozen cycles, so I am not sure what that means? I thought it would popup at least once per cycle or at worst 45000 times per cycle (or something like that.)


  •  

    Posting Permissions

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