(I hope this is the right section, -since it is javascript...) I have some code that refreshes/loads a php page in another php page every 2000milisceonds (which just echos "Hello World" for now -its the concept i need to understand first) -So it doesnt refresh whole page. And i think it doesnt work, -after 2000milisceonds it loads the phppage, then after another 2000, i think it tries to load it twice, and then three times etc... And in the end (20 seconds later) i just get a flashing 'Hello world' then it lags and finally my Firefox crashed... -Its probably a small mistake in this code... (Can anyone please fix it) <html> <body> <script type="text/javascript"> function Ajax(){ var xmlHttp; try{ xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari }catch (e){ try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer }catch (e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch (e){ alert("No AJAX!?"); return false; } } } xmlHttp.onreadystatechange=function(){ document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText; setTimeout('Ajax()',2000); } xmlHttp.open("GET","refreshtest2.php",true); xmlHttp.send(null); } window.onload=function(){ setTimeout('Ajax()',2000); } </script> <div id="ReloadThis"></div> </body> </html> Thanks alot, James
****Updated**** I have sort of fixed the last problem, -it doesnt flash loads... This ajax code works to refresh every 2 seconds: However, it flashes the 'Hello World' (on refreshtest2.php) every 2 seconds, so it will load it, and after 2seconds make it blank and then load it again (Appears to flash every 2 seconds) How can I fix this? <script type="text/javascript"> function Ajax(){ var xmlHttp; try{ xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari }catch (e){ try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer }catch (e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch (e){ alert("Your browser does not support this webpage sorry..."); return false; } } } xmlHttp.onreadystatechange=function(){ document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText; } xmlHttp.open("GET","refreshtest2.php",true); xmlHttp.send(null); } window.onload=function(){ setInterval('Ajax()', 2000); } </script> <div id="ReloadThis"></div> Thanks alot, James
Hey, to fix the flashing you simply need to check on what statuscode your AJAX request is. Now you replace it no matter the status or the content. So simple change your function: xmlHttp.onreadystatechange=function(){ document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText; } Code (markup): To: xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState == 4) { document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText; } } Code (markup): Good luck