I'm having a problem in JS where my script times out after around 10 minutes of running. The script is in 3 parts, one is the display page, the next is the ajax.js file which calls to rpc.php file. The ajax.js has a timeout function which calls the rpc.php every 5 seconds. The rpc.php outputs an echo like: "divide|new information to be displayed" with the first part identifying the div id on the display page where the second half is to be placed. After around 10 minutes the script just stops.. I can refresh the page to start it again without problem but i need to be able to run the script for a long time. The script shows information kind of like the DP spy which is constantly changing.
My html page just starts the clock in the following javascript function createRequestObject() { var ro; var browser = navigator.appName; if(browser == "Microsoft Internet Explorer"){ ro = new ActiveXObject("Microsoft.XMLHTTP"); }else{ ro = new XMLHttpRequest(); } return ro; } var http = createRequestObject(); function sndReq(action) { http.open('get', 'rpc.php?action='+action); http.onreadystatechange = handleResponse; http.send(null); } function handleResponse() { if(http.readyState == 4){ var response = http.responseText; var update = new Array(); if(response.indexOf('|' != -1)) { update = response.split('|'); document.getElementById(update[0]).innerHTML = update[1]; } } // } var timerID = null var temp = null var temp2 = null //var timerID = null var timerRunning = false function stopclock(){ if(timerRunning) clearTimeout(timerID) timerRunning = false } function startclock(){ // Make sure the clock is stopped stopclock() showtime(1) } function showtime(){ sndReq('run') timerID = setTimeout("showtime()",5000) timerRunning = true } Code (markup): So basically every 5000 milliseconds it calls the function sndReq which triggers my php file. The php file does its stuff then outputs divname|information to go inside the div. The javascript then parses this sets things correctly in the original html file. The problem is that the showtime function seems to stop running after a while.
I tried the script for about 22 minutes. It worked fine and made about 260 requests without any problem before I stopped it. And i went through your code too, but couldn't find any problem. I don't any problem in the above script. If you have opera or firefox, check the error console to find whether any error is being reported or not. I have one suggestion, try to use object detection for creating the ajax object. Browser detection is no longer used as it cannot be relied upon always.
I was thinking that perhaps the php is taking longer then 5 seconds to complete sometimes (it fetches a table of 20 rows with 5 columns). If the php is still processing when the java calls to it again would that create the problem? Could you send me a link to some working object detection code? I'm not too up on ajax, I just use the same code everytime Then again it lets me use php without reloading so its still good.
You can use the following code for creating the request object by object detection. function createRequestObject() { var ro; if(window.XMLHttpRequest) { ro = new XMLHttpRequest(); }else{ try{ ro = new ActiveXObject('MSXML2.XMLHTTP'); }catch(e){ try { ro = new ActiveXObject('Microsoft.XMLHTTP'); }catch(e){ // Unsupported Browser return false; } } } return ro; } Code (markup):
I used the sleep command to exend the execution time of rpc.php to more than 5 seconds. The script makes 2 send request before it recieves the return data. And it worked without stopping. If you have Opera or Firefox, check the Error console to see whether the problem is with Javascript or not.
Now we're getting somewhere The first time i ran it i received an two errors, one that said update[0] is undefined.. so I changed update[0] (which will be the divide's id) with a static id reference. The second error was the one below, I no longer receive the first error after changing it to a static id reference. I do still receive the second error though... uncaught exception: [Exception... "Component returned failure code: 0xc1f30001 (NS_ERROR_NOTE_INTITIALIZED) [nsIXMLHttpRequest.send]" nsresult: "0xc1f30001 (NS_ERROR_NOT_INITIALIZED)" location: "JS frame :: http://myinternetaddress.com/ajax.js :: sndReq :: line 17" data:no]
I can't paste the code.. The php fetches the last 20 changed rows in a table (via mysql) and puts them into a <table> format and pops them into the div. Could it be a mysql timeout from too many mysql connections.. maybe i need to close the mysql connection each time...
I don't think the problem is with MySQL or the PHP, since we are getting a JavaScript error. So try inserting an alert(http) statement before and after the http.send statement.