SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    setTimeout and IE6

    I have posted this in a couple other forums and can't seem to find an answer to my problem..

    This is a relatively minor issue but I refuse to give up on it.

    Ok, I have a simple ajaxian set of functions that retrieve the current date from a backend file.

    The code below works perfectly in FF, Opera and Safari..

    Code:
    function createRequestObject() {
        if (window.XMLHttpRequest) { // Mozilla, Safari, Opera...
            var xmlhttp = new XMLHttpRequest();
            if (xmlhttp.overrideMimeType)
    	    xmlhttp.overrideMimeType('text/xml');
        }
    	else if (window.ActiveXObject) { // IE
            try {
                var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
    
        if (!xmlhttp) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
    	return xmlhttp;
    }
    
    var http = createRequestObject();
    var dtimer;
    
    
    function sendRequest(option) {
         if (option !== 'first') {
    	 var stopTimer = timer('stop');
         }
         http.open('GET', 'includes/process.php?action=getTime');
         http.send(null);
         try {
    	   http.onreadystatechange = handleRequest;
         } catch (error) {
    	   alert('the ready state is never changed');
         }
    }
    
    function handleRequest() {
          if (http.readyState == 4) {
    	  var response = http.responseText;
    	  document.getElementById('time').innerHTML = response;
    	  var startTimer = timer('start');
          }
    }
    
    function timer(state) {
         if (state == 'start') {
    	 var browser = navigator.appName;
    	 if (browser == 'Micorsoft Internet Explorer') {
    	     http = null;
    	     http = createRequestObject();
    	     try {
    		  setTimeout("sendRequest('second')", 500);
    	     } catch (e) {
    		  alert('The Time out is not being set');
    	     }
    	 } else {
    	     dtimer = setTimeout("sendRequest('second')", 500);
    	 }
         } else {
    	clearInterval(dtimer);
         }
    }
    I have traced it down to the fact that the handleRequest function is never being called in IE6..

    As I said it works fine in every other browser except MicroShaft's..

    This will work if I use setInterval but it blinks in and out every other second so that is not really an option..

    Any suggestions would be great.

    Thanks,
    Tom

  2. #2
    SitePoint Addict
    Join Date
    Oct 2006
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My projects have no problems performing Ajax operations. Here's a snippet of the code I use:

    PHP Code:
        //  Configure the XMLHttpRequest object
        
    this.httpConnection.onreadystatechange forwardStateChange;
        try 
            { 
    this.httpConnection.openrequestTypeajaxUrltrue ); }
        catch (
    exception
        {
            
    //  URL is not in the same domain as the page        ---------------------------------
            
    window.alert'Target URL (' ajaxUrl ') is not in the same domain as the page.' );
            return 
    false;
        }
        
    this.httpConnection.setRequestHeader'Content-Type''application/x-www-form-urlencoded; charset=UTF-8' );
        
        
        
    //  Send the request  --  Need to indicate _busy_ to the user!
        
    this.httpConnection.sendrequestParams ); 
    This code actually sends a parameterized request to the server. Since you aren't sending parameters, you might be able to use simpler code.

    My biggest observation is your open() function call. Try adding a third parameter of true - make an asynchronous request. I suspect that Microsoft defaults the value to false - a synchronious request. A synchronous request will not call the onreadystatechange function.

    Hope this helps.....

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to declare the onreadystatechange handler before you call the send method.


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
  •