Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ajax help please??

Status
Not open for further replies.

kyle83

IS-IT--Management
Aug 4, 2006
8
0
0
Hi everyone,

I'm having some trouble writing a function that periodically checks the user's connection. Basically I want to use setInterval to call the follownig function every 15 seconds or so.. The function will try and load a blank page (null.php). If it gets no response (readstate doesn't reach 4), then i need it to pop up a notification layer to alert the user that the connection has failed. I thought of implemeting a timer somewhere inside the function (if it doesn't receive a response for so many seconds then show the alert). But, I'm not quite sure how to do this because I'm thinking the setInterval would need to be extended to wait for the function (if its taking a bit long to receive a response).

Can anyone give me a tip on how to beef up this script to pull this off? Or If anyone has a completely better way of doing this, that would be appreciated too.. Thanks in advance everyone..

function isConnected(){
var thepage = 'null.php';
var xhReq = createXMLHttpRequest();
xhReq.open("GET",'pages/' + thepage,true);
xhReq.onreadystatechange = onSumResponse;
xhReq.send(null);

function onSumResponse(){
if (xhReq.readyState != 4){
return;
}
else{
// Is Connected, No Alert
}
}
}
 
i was asking a similar question recently in the ajax forum and got no response. so your question piqued my interest. here is some rough code i knocked up from yours above. i've not checked it and i'm really not a javascript programmer! I think that the logic works though. you'd start the ball rolling with startTimer(2000) on the body onload event.

JavaScript:
var xhReq = createXMLHttpRequest();
var timerVal = 0;
var intervalTimer = ''; 
var offline = false;

function createXMLHttpRequest() {  //taken from [URL unfurl="true"]http://www.dynamicajax.com/fr/AJAX_Hello_World-271_290_322.html[/URL]

	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //Not IE
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} else {
		alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade.");
	}
}
function startTimer(speed){
	intervalTimer = window.setInterval('checkState()', speed);
}
function testConnection(){
	timerVal = 0; //reset the timer
        var thepage = 'null.php';
        xhReq.open("GET",'pages/' + thepage,true);
        xhReq.send(null);
}

function checkState(){
	switch (xhReq.readyState == 4) {
		case 4:
			if (offline == true) {
				offline = false;//reset the offline flag to show online
				alertuser_online();
				startTimer(2000);
			}
			testConnection(); //start the cycle again
			break;
		case 0:
			testConnection(); //start the cycle
			break;
		default:
			if (timerVal > 15){
				startTimer(6000); //slow down the recheck			
				if (offline == false){
					xhReq.abort; //cancel the request to reset the readystate for next time round
					offline = true;
					alertUser_offline();
				}
			} else {
				timerVal++;
			}
	} //close the switch
}
function alertUser_offline(){
	alert('Your connection has gone off line');
}
function alertUser_online() {
	alert ('Your connection is back online');
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top