www.webdeveloper.com
Results 1 to 2 of 2

Thread: setTimeout() reloads instantly

Hybrid View

  1. #1
    Join Date
    May 2009
    Posts
    1

    setTimeout() reloads instantly

    Hello there,

    I am having some problem with JavaScript's setTimeout() function.

    The result I want to achieve is this:
    Master page reloads the slave page every few seconds and the slave page redirects to the master page each time it's loaded. I know it's hard to comprehend why it's needed, but it is.

    The problem I'm having is this:
    The master page doesn't wait the given seconds to reload the slave page, but reloads it instantly. And the slave page does as it should and redirects to the master loop which by itself makes an endless loop of instantaneous redirections.

    Master page javascript ($sid variable exists and is recognized):
    Code:
    <script type="text/javascript">
    <!--
    location.href = '?m=history&sid=$sid&a=refresh';
    
    function timedRefresh(timeoutPeriod) {
    setTimeout("window.location.reload(true);",timeoutPeriod);
    }
    
    //-->
    </script>
    Here, the function timedRefresh() is called on the master page html like so:
    Code:
    <body onload="timedRefresh(5000)">
    As I understand, it should wait 5 seconds, load the slave page, which by itself redirects to the master page and the wait begins again. For some reason, it doesn't work like that.

    The slave page works fine, you don't have to worry about that.

    I would greatly appreciate any help.

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    BTW, HTML comments in scripts aren't needed anymore.
    Code:
    <script type="text/javascript">
    location.href = '?m=history&sid=$sid&a=refresh';
    function timedRefresh(timeoutPeriod) {
    setTimeout("window.location.reload(true);",timeoutPeriod);
    }
    </script>
    What I've bolded is certainly changing the page, are you sure that that's not what you are mistaking for reload? Everytime the script gets to that line, ?m=history&sid=$sid&a=refresh will be loaded, so in effect, the timedRefresh function is never loaded at all.

    And, in general, when using setTimeout or setInterval, using a function is preferable.
    Code:
    setTimeout(function(){window.location.reload(true);},timeoutPeriod);

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles