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 AjaxObject() { var self = this; this.oXMLHttp = this.getXMLHttpRequest(); this.bHasBeenInteractive = false; this.fCallBack = null; this.oXMLHttp.onreadystatechange = function() { self.handleReadyStateChange(); }; } AjaxObject.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); } else { alert("request completed but, never went through the correct stages"); } } } AjaxObject.prototype.getXMLHttpRequest = function() { /* * 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; } 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 + "
\n"; } function startAjaxRequest2() { if(giTimerId2 !== 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 + "
\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 oAjaxObj = new AjaxObject(); oAjaxObj.fCallBack = fCallBack; oAjaxObj.oXMLHttp.open("GET", sURL, true); oAjaxObj.oXMLHttp.send(null); }