All pastes #438173 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-04-13 13:51 UTC
rendered paste body
var XMLHTTP_UNITIALISED = 0;
var XMLHTTP_LOADING = 1;
var XMLHTTP_LOADED = 2;
var XMLHTTP_INTERACTIVE = 3;
var XMLHTTP_COMPLETED = 4;

var giTimerId = null;
var giTimerId2 = null;

function XMLHttpRequestWrapper(oObject)
{
    this.oXMLHttp = oObject;
    this.bHasBeenInteractive = false;
    this.fCallBack = null;
}

XMLHttpRequestWrapper.prototype.handleReadyStateChange = function()
{
    /*
     * Author: Chris Miles
     * Name: handleReadyStatusChange
     * Purpose: Called when the status of the XMLHttpRequest object's state changes.
     * Params: none
     * Returns: none
     */
    
    if((this.oXMLHttp.readyState === XMLHTTP_INTERACTIVE))
    {
        this.bHasBeenInteractive = true;
    }
    if((this.oXMLHttp.readyState === XMLHTTP_COMPLETED))
    {
        if(this.bHasBeenInteractive === true)
        {
            this.fCallBack(this.oXMLHttp.responseText);
            this.oXMLHttp.onreadystatechange = null;
        }
        else
        {
            alert("request completed but, never went through the correct stages");
        }
    }
}

function startAjaxRequest()
{
    if(giTimerId !== null)
    {
        clearTimeout(giTimerId);
    }
    
    var oDate = new Date();
    ajaxRequest("/sandbox/test.php?time=" + oDate.getTime(), processAjaxResponse);
    //giTimerId = setTimeout("startAjaxRequest();", 1000);
}

function processAjaxResponse(sReponseText)
{
    document.getElementById("output").innerHTML += "timer 1: " + sReponseText + "<br>\n";
}

function startAjaxRequest2()
{
    if(giTimerId !== null)
    {
        clearTimeout(giTimerId2);
    }
    
    var oDate = new Date();
    ajaxRequest("/sandbox/test.php?time=" + oDate.getTime(), processAjaxResponse2);
    //giTimerId2 = setTimeout("startAjaxRequest2();", 2000);
}

function processAjaxResponse2(sReponseText)
{
    document.getElementById("output2").innerHTML += "timer 2: " + sReponseText + "<br>\n";
}

function ajaxRequest(sURL, fCallBack)
{
    /*
     * Author: Chris Miles
     * Name: ajaxRequest
     * Purpose: starts a AJAX request.
     * Params: sURL - String - URL to fetch
     *         fCallBack - Function - address of function to be executed
                           upon completion of request.
     * Returns: none
     */
    
    // need a clean way of creating new objects and cleaning them up
    // possible a global area gaRequests[gaRequests.length] = request;
    
    var oXMLHttp = getXMLHttpRequest();
    
    var oXMLHttpWrap = new XMLHttpRequestWrapper(oXMLHttp);
    
    if(oXMLHttpWrap.oXMLHttp !== null)
    {
        oXMLHttpWrap.oXMLHttp.onreadystatechange = function()
        {
            oXMLHttpWrap.handleReadyStateChange();
        };
        
        oXMLHttpWrap.fCallBack = fCallBack;
        oXMLHttpWrap.oXMLHttp.open("GET", sURL, true);
        oXMLHttpWrap.oXMLHttp.send(null);
    }
}

function getXMLHttpRequest()
{
    /*
     * Author: Chris Miles
     * Name: getXMLHttpRequest
     * Purpose: Gets a XMLHTTP Object for the current browser.
     * Params: none
     * Returns: oXMLHttp - Object - XMLHTTP object
     */
    
    var oXMLHttp = null;
    if(typeof XMLHttpRequest !== "undefined")
    {
        oXMLHttp = new XMLHttpRequest();
    }
    else
    {
        try
        {
            oXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(oException)
        {
            try
            {
                oXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(oException)
            {
                alert("Error creating oXMLHttp");
            }
        }
    }
    return oXMLHttp;
}