SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to keep this firing

    Hello, here's a working http request. If you upload it named as anything.html along with a page named test.html, it will call whatever text you put in test.php It will put the text in the div id named T1.
    <html>
    <head>
    <script type="text/javascript">
    function myNewXMLobject(){
    var xmlhttp=false;
    try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
    xmlhttp = false;
    }
    }
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    try {
    xmlhttp = new XMLHttpRequest();
    } catch (e) {
    xmlhttp=false;
    }
    }
    if (!xmlhttp && window.createRequest) {
    try {
    xmlhttp = window.createRequest();
    } catch (e) {
    xmlhttp=false;
    }
    }
    return xmlhttp;
    }
    xmlobject=myNewXMLobject();
    function up()
    {xmlobject.open("GET", "test.php",true);
    xmlobject.onreadystatechange=function() {
    if (xmlobject.readyState==4) {
    document.getElementById('T1').innerHTML=xmlobject.responseText;
    alert(xmlobject.responseText)
    }
    }
    xmlobject.send(null)
    }
    </script>
    </head>
    <body onload="up();">
    <div id="T1" style="border:1px solid black;height:100;width:150"></div>
    </body>
    </html>
    I need to know how to keep the request firing. So that after it pulls the page it will pull it again after so much time. I tried setInterval(' ',10000); but I didn't know where to put it and which function to use for it. I also didn't know if the body onload was keeping it from firing again. Please get back to me, I can really use some help on finishing this, thank you very much.
    Last edited by 1Jen; Apr 24, 2007 at 02:54.

  2. #2
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about adding a setTimeout at the end of your up() function:
    Code:
    function up() {
      xmlobject.open("GET", "test.php",true);
      xmlobject.onreadystatechange=function() {
        if (xmlobject.readyState==4) {
          document.getElementById('T1').innerHTML=xmlobject.responseText;
          alert(xmlobject.responseText);
          setTimeout("up",5000); //run this again in 5 seconds
        }
      }
      xmlobject.send(null)
    }
    var me = null;

  3. #3
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or use interval, since ajax request may sometime fail. hence break the period.


    Code:
    setInterval(callFunc, 1000);  // 1 seconds
    
    
    done = false;
    lastfiring = (new Date()).getUTCSeconds();
    
    function callFunc() {
     newfiring = (new Date()).getUTCSeconds();
    timeout = newfiring - lastfiring > 6; // 6 seconds
     if (done || timeout) {
       done = true; 
       return;
     }
    lastfiring = newfiring;
    
      xmlobject.open("GET", "test.php",true);
      xmlobject.onreadystatechange=function() {
        if (xmlobject.readyState==4) {
          document.getElementById('T1').innerHTML=xmlobject.responseText;
          alert(xmlobject.responseText);
          done = true;
        }
      }
      xmlobject.send(null)
    }

  4. #4
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I reeaaaally appreciate the help. The first one is only showing the first page more than once, its not repulling it all over again. The second one is not refiring yet but don't know why. On the second one I'm putting this function in the onload.<body onload="function();"> Let me know if I have that function listed wrong. Thank you very much.
    Last edited by 1Jen; Apr 29, 2007 at 04:50.

  5. #5
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anyone know how to re-execute a http request? Don't have one working yet, and need some help, thanks.


Bookmarks

Posting Permissions

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