DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: ajax and set.interval problem

  1. #1
    Join Date
    Oct 2008
    Posts
    1

    ajax and set.interval problem

    Code:
    <script type="text/javascript">
    var http = false;
    
    
      http = new XMLHttpRequest();
    function curPer() {
      http.abort();
      http.onreadystatechange=function() {
        if(http.readyState == 4) {
          document.getElementById('cp').innerHTML = http.responseText;
        }
      }
      http.open("GET", "validate.php?name=1", true)
      http.send(null);
    }
    
    </script>
    <div id="cp">adfaf</div>
    
    <script type="text/javascript">
    x = setInterval (curPer(), 1000);
    
    
    </script>
    although the setinterval works the first time to replace the adfaf with the response text the first time, it fails to do so repeatedly. any ideas?

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Welcome to DevX

    What happens on subsequent calls?

  3. #3
    Join Date
    Oct 2008
    Posts
    5
    If you run it in firebug, you get this:

    useless setInterval call (missing quotes around argument?)
    [Break on this error] x = setInterval (curPer(), 1000);

    So change it to:
    Code:
    <script type="text/javascript">
    var http = false;
    
    
      http = new XMLHttpRequest();
    function curPer() {
      http.abort();
      http.onreadystatechange=function() {
        if(http.readyState == 4) {
          document.getElementById('cp').innerHTML = http.responseText;
        }
      }
      http.open("GET", "validate.php?name=1", true)
      http.send(null);
    }
    
    </script>
    <div id="cp">adfaf</div>
    
    <script type="text/javascript">
    x = setInterval (function () { curPer() }, 1000);
    </script>
    Or you can do what FireBug said was wrong and just put

    Code:
    x = setInterval ("curPer()", 1000);
    But personally i don't like that and always use anonymous functions (allowing you to expand them in the future easily and look more consistent).

    Kind regards,
    Scott

  4. #4
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    Hi, adding to what Scott said, I'll start by explaining how it worked the first time. setInterval expects a function, or a string that contains code. Using a function is better than using the string version. When the interpreter sees the line saying
    x = setInterval (curPer(), 1000);
    it evaluates (runs) curPer() first. That is why you get the result the first time. Then, a syntax error occurs because curPer() doesn't return a function. (The setInterval's expectation). So, if you put a script line (like an alert()) below it, you will see that it won't be executed.
    To fix, just remove the brackets of curPer!
    Code:
    x = setInterval (curPer, 1000);
    This gives the function to setInterval, rather the return value of the function. You may want to increase the timeout if you are going to use this in a big network.

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center