1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

setInterval / setTimeout Not consistantly working

Discussion in 'JavaScript' started by jpny, Dec 20, 2022.

  1. #1
    I have a page that is a dashboard for several IOT devices. The page auto-refreshes every 10 min. I often leave the page open in my browser for days at a time. When the computer wakes from sleep it takes the network card a few extra seconds to fully wake up, so I added a timer check that adds another 10 seconds if the page has not been refreshed in over 12 min (indicating that is was in sleep mode).

    This has been working fine for years. Now suddenly after the computer wakes up 50% of the time the page does not refresh at all (page not found browser message) until I refresh the page manually, then it returns to auto-refresh. I think this is because the auto refresh is in the page load, so if no page is loaded then the auto-refresh isn't loaded (so this isn't really my problem, just mentioning it to further describe the issue).

    I have not changed anything on my computer (same version of browser, no network driver or any driver updates, etc) and I have even tried in several other browsers that I don't normally use and the refresh problem is present intermittently in all browsers. I have also tried on several different computers. I cant say for sure that the problem wouldn't have been present in the other browsers, or other computers, but I can say for sure that in the browser I use for that page it has been working for years (and I never update that version of the browser).

    I have tried increasing all of the timer values - didn't help
    Could something have change on the hosting company side?

    This is the code, but it has been working fine for years
    (I don't worry about resetting the setInterval because i assume it is reset when the page refreshes anyway)
    
    <html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    </head>
    
    <body onload="var lastUpdate=getTick(); setInterval(function(){refreshCheck(lastUpdate)},600000)"  style="overflow:hidden;margin:0">
    
      <iframe src="sensor1.htm" width="100%" height="137" frameBorder="0"  scrolling="no"></iframe>
    
    <script>
      function getTick(){
      var d = new Date();var n = d.getTime();return n;
      }
    
      function refreshPage(){
      location.reload(true);
      }
    
      function refreshCheck(lastUpdate){
     
      if (getTick() - lastUpdate > 720000)
       {setTimeout (refreshPage,100000);}
      else {
       refreshPage();}
      }
    </script>
    
    </body>
    </html>
    
    Code (markup):

     
    Last edited by a moderator: Dec 21, 2022
    jpny, Dec 20, 2022 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    Sounds like a page error and there's actually one page should be checked, the sensor1.htm in your iframe.
    Have you tried testing this sensor1.htm separately and check whether it is always okay, even after few days?
     
    hdewantara, Dec 20, 2022 IP
  3. jpny

    jpny Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Interesting thought, but I actually have a "main page" that loads 6 of these (sensor 1-6), each in an iframe. The refresh interval is slightly staggered so they refresh several seconds apart. The main page does not refresh, just the individual iframes. Doubt that all 6 sensors would be offline at the same time. They are never offline when doing a manual refresh or when auto-refresh is working (just sometimes after waking the computer - but as I said i have doubled the wait time after sleep and still have the issue sometime - never had an issue prior to a few months ago)
     
    jpny, Dec 20, 2022 IP
  4. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #4
    Interval is being set to smaller value than what is being checked against. Let's simplify it and force iframe to reload page by adding some garbage in querystring parameters. I simplified it using jQuery, I hope that is fine too.
    
    <html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    </head>
    <body style="overflow:hidden;margin:0">
        <iframe id="ifr" src="sensor1.htm" width="100%" height="137" frameBorder="0" scrolling="no"></iframe>
        <script>
            setInterval(
                function() 
                {
                    //force server to reload
                    $('#ifr').attr('src', 'sensor1.htm?r='+Math.random(999999));
                }, 
                720000
            );
        </script>
    </body>
    </html>
    
    Code (markup):
    I hope it helps.

    Stay well....
     
    Vooler, Dec 20, 2022 IP
  5. jpny

    jpny Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    I appreciate the effort but this code doesn't take into account the extra time it take for the NIC to wake up, it just checks every 12 min. which is going to create its own problems. All you are really doing is adding a random number to the URL to make it unique, which I dont think is the issue. Im not getting a cached page (stale data), Im getting page not found.
     
    jpny, Dec 21, 2022 IP
  6. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #6
    hmm I have no idea either, sorry.
    Is it possible that the body.onload codes doesn't get executed sometimes because those they haven't defined yet e.g. refreshCheck() defined later in <script> block? But why does it generate "page not found" error?
    Anyways, I might try something like the following:
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        </head>
    
        <body style="overflow:hidden;margin:0">
            <iframe src="sensor1.htm" width="100%" height="137" frameBorder="0"  scrolling="no"></iframe>
    
            <script>
                //defines       
                var lastUpdate = getTick();
    
                function getTick(){
                    var d = new Date(),
                        n = d.getTime();
                    return n;
                }
    
                function refreshPage(){
                    location.reload(true);
                }
    
                function refreshCheck(){
                    if(getTick() - lastUpdate > 720000)
                        setTimeout(refreshPage, 100000);
                    else
                        refreshPage();
                }
               
                //main
                setInterval(refreshCheck, 600000);
            </script>
        </body>
    </html>
    HTML:
    The difference to your original code is that everything is defined first before usage. No more onload.
     
    hdewantara, Dec 21, 2022 IP
  7. jpny

    jpny Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    Thanks. I substituted your code, but ant the moment I cant get the iframe with my code or your code to screw up. Will report back in a few days when I get more feedback.
     
    jpny, Dec 22, 2022 IP
  8. jpny

    jpny Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    When I fired up the computer this morning (after it was hibernating over night) the only iFrame that failed was the one with the new modified code above from hdewantara. So I guess that didn't help. Its weird, I haven't been able to get my code to fail for the last few days. Last week it was failing a lot
     
    jpny, Dec 23, 2022 IP
  9. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #9
    When there's failure, could you post here about any errors / warnings reported by your browser?
    I think that is accesssible with an F12 keypress and try to find the CONSOLE tab...
     
    hdewantara, Dec 25, 2022 IP
  10. jpny

    jpny Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #10
    Haven't had any issues in 2 weeks. I'm back to the original code which as I said earlier was working perfectly for years. Maybe it was something on the hosting side as nothing changed on my end.

    Thanks everyone for your suggestions. Will post back if the problem returns
     
    jpny, Jan 4, 2023 IP