setTimeout in IE

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lcthree
    New Member
    • Oct 2008
    • 6

    setTimeout in IE

    I'm trying to write an AJAX application which periodically updated some data, here's the outline of my code, cut down to the important bits:

    Code:
    ...
    
    var http = createRequestObject();
    
    function createRequestObject() {
        var ro;
        var browser = navigator.appName;
        if (browser == "Microsoft Internet Explorer") {
            ro = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            ro = new XMLHttpRequest();
        }
        return ro;
    }
    
    function startup() {
        // Do Stuff
        update();
    }
    
    function update() {
        http.open('get', 'test.php');
        http.onreadystatechange = updated;
        http.send(null);
    }
    
    function updated() {
        if (http.readyState == 4) {
            // Do More Stuff
        }
        setTimeout(update, 500);
    }
    
    ...
    
    <body onload="startup();">
    
    ...
    My problem is that on IE, the setTimeout event never fires, so update is called once when called by startup(), but then never again. On other browsers it works fine. I've tried replacing setTimeout(upda te, 500); with setTimeout('upd ate()', 500); but that didn't work either.

    Can anyone suggest what I'm doing wrong?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    It'll probably be caused by caching. To confirm, add an alert to your update() function.

    PS. there's no need for browser detection in your createRequestOb ject function.

    Comment

    • lcthree
      New Member
      • Oct 2008
      • 6

      #3
      yes it was casheing, thanks, i have it fixed now.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        No problem. Glad you got it to work :)

        Comment

        Working...