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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    278
    Thanks
    78
    Thanked 2 Times in 2 Posts

    Question Ajax to Check DB loop

    I am trying to get this script to check the database repeatedly for any changes. It works, then after refreshing my site a couple times, it just stops. I have no idea what is happening. It works when I reopen the site. Does anyone know what is wrong?

    Code:
    <script>
    function CheckInbox(uid)
    {
    if (uid=="")
      {
      document.getElementById("txtHint").innerHTML="";
      return;
      } 
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","../Js/CheckInbox.php?uid="+uid,true);
    xmlhttp.send();
    }
    </script>
    
    <script>setInterval(function(){CheckInbox(<?php echo $uid; ?>)}, 2000)</script>
    
    <div id="txtHint"></div>
    Last edited by Ndogg; 07-20-2013 at 11:45 AM.

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    you might want to try a recursive settimout instead : read this : http://www.erichynds.com/blog/a-recu...imeout-pattern

  • Users who have thanked DanInMa for this post:

    Ndogg (07-21-2013)

  • #3
    Regular Coder
    Join Date
    Jun 2009
    Posts
    278
    Thanks
    78
    Thanked 2 Times in 2 Posts
    Thankyou! That seems to be working.

    Although, it seemed to really increase the CPU usage for my site? Does that sound correct?
    Last edited by Ndogg; 07-21-2013 at 10:47 AM.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You way want to consider web sockets.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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