Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Function won't repeat

  1. #1
    Join Date
    Oct 2006
    Location
    Shanghai, China
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Function won't repeat

    I'm trying to get this function to repeat once every second.

    It's supposed to:
    1. copy the text off of the webpage
    2. split it at the '|'
    3. add the second string to the document
    4. replace the variable in the function's bracket with the first string from the split
    5. and repeat the process after 1000 miliseconds.

    Code:
    function getPage(t) {
    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') {
                    xmlhttp = new XMLHttpRequest();
            }
    
            var file = 'http://webpage.com/page.php?t=';
        xmlhttp.open('GET', file + t, true);
        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4) {
                    var content = xmlhttp.responseText;
                    var bit = content.split("|");
                    document.getElementById('text').innerHTML += bit[1];
            }
            }
            xmlhttp.send(null);
    return;
    r=setTimeout("getPage(bit[0])",1000);
    }
    Everything seems to work fine until it's time to repeat. Then I get an error that says 't' is undefined

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    You need to place the return statement after the timeout.
    Trinithis

  3. #3
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    And bit[0] needs to be outside of the string, like this:
    Code:
    r=setTimeout("getPage('"+bit[0]+"')",1000);
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Or, the way real coders do it:

    Code:
    r=setTimeout(function(){getPage(bit[0]);},1000);
    What's r?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Or, the way real coders do it:
    Or sane people.
    What's r?
    A numerical identifier for the timeout request (used with clearTimeout() &c.).

    Neither of these solutions will work, of course, because "bit" is local to the onchange function.
    Code:
    var Xhr = (function() {
      var xhr = null;
    
      function newXhr() {
        if(window.XMLHttpRequest)
          return new XMLHttpRequest();
        else if(window.ActiveXObject)
          try {
            return new ActiveXObject("Msxml2.XMLHTTP");
          } catch(e) {
            return new ActiveXObject("Microsoft.XMLHTTP");
          }
        return null;
      }
    
      function getXhr() {
        return xhr || (xhr = newXhr());
      }
    
      function getFile(url, callback) {
        var xhr = getXhr();
        xhr.open("GET", url, !!callback);
        if(callback)
          xhr.onreadystatechange = function() {
            if(this.readyState === 4)
              return callback(this.responseText);
          };
        xhr.send(null);
        return callback ? undefined : xhr.responseText;
      }
    
      return {
        newXhr: newXhr,
        getXhr: getXhr,
        getFile: getFile
      };
    })();
    
    // I'm not sure what this function should be called, but
    // "getPage" is woefully inaccurate.
    function awakenLateParrot(t) {
      Xhr.getFile("http://webpage.com/page.php" + (t ? "?t=" + t : ""),
        function(text) {
          // Hurry up o ye days of destructuring assignment and
          // (nextT, toAppend) = text.split("|"); // :-(
          var nextT = text.split("|"),
            toAppend = nextT[1];
          nextT = nextT[0];
    
          document.getElementById("text").appendChild(document.createTextNode(toAppend));
          setTimeout(function() { awakenLateParrot(nextT); }, 1000);
        });
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    A numerical identifier for the timeout request (used with clearTimeout() &c.).
    Numerical? Don't you mean alphabetic?
    Trinithis

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No, numerical. It's an integer.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Yes, r would be a number, but it is undeclared and in the global scope, so it's an error waiting to happen. I asked what it was to get at that fact, not because I didn't know what sort of value it would (if unencumbered by errors) be assigned.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Oct 2006
    Location
    Shanghai, China
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    What's r?
    I don't know... I'm pretty weak on JavaScript, but I'm trying to learn. I just used what I saw on www.w3schools.com to see if I could splice it together and make it work.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function timedMsg()
    {
    var t=setTimeout("alert('5 seconds!')",5000);
    }
    </script>
    </head><body>
    <form>
    <input type="button" value="Display timed alertbox!"
    onClick="timedMsg()">
    </form>
    </body>
    </html>
    In this case, I just took the function and replaced t (which is also undeclared, isn't it?) with r since I was already using t as a variable at the time.

    Thank you Twey, that seems to fix that problem. Onto the next bug...
    Last edited by kasei; 12-30-2007 at 01:05 PM.

  10. #10
    Join Date
    Oct 2006
    Location
    Shanghai, China
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Well...

    I don't get any more errors, but it doesn't display anything at all now!

    Code:
    <html>
    <head>
    <title>Test</title>
    
    <script type="text/javascript">
    var Xhr = (function() {
      var xhr = null;
    
      function newXhr() {
        if(window.XMLHttpRequest)
          return new XMLHttpRequest();
        else if(window.ActiveXObject)
          try {
            return new ActiveXObject("Msxml2.XMLHTTP");
          } catch(e) {
            return new ActiveXObject("Microsoft.XMLHTTP");
          }
        return null;
      }
    
      function getXhr() {
        return xhr || (xhr = newXhr());
      }
    
      function getFile(url, callback) {
        var xhr = getXhr();
        xhr.open("GET", url, !!callback);
        if(callback)
          xhr.onreadystatechange = function() {
            if(this.readyState === 4)
              return callback(this.responseText);
          };
        xhr.send(null);
        return callback ? undefined : xhr.responseText;
      }
    
      return {
        newXhr: newXhr,
        getXhr: getXhr,
        getFile: getFile
      };
    })();
    
    // I'm not sure what this function should be called, but
    // "getPage" is woefully inaccurate.
    function awakenLateParrot(t) {
      Xhr.getFile("http://test.game.joescuriosityshoppe.com/chat/getposts.php" + (t ? "?t=" + t : ""),
        function(text) {
          // Hurry up o ye days of destructuring assignment and
          // (nextT, toAppend) = text.split("|"); // :-(
          var nextT = text.split("|"),
            toAppend = nextT[1];
          nextT = nextT[0];
    
          document.getElementById("text").appendChild(document.createTextNode(toAppend));
          setTimeout(function() { awakenLateParrot(nextT); }, 1000);
        });
    }
    </script>
    
    </head>
    <body onload="awakenLateParrot('0')">
    <div id="text"></div>
    </body>
    </html>
    I've set getposts.php to display a 4 character number, followed by "|<div>hello</div>", no matter if t has any value at all.

    If it worked, then "hello" should be displayed and the function should repeat after 1000 miliseconds with the 4 character number as the new t, right?

    All I get is a blank screen.

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
  •