1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help: Simple autorefresh (Ajax)

Discussion in 'JavaScript' started by Skillman13, Jan 1, 2010.

  1. #1
    (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
     
    Skillman13, Jan 1, 2010 IP
  2. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    ****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
     
    Skillman13, Jan 2, 2010 IP
  3. Randombase

    Randombase Peon

    Messages:
    224
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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 ;)
     
    Randombase, Jan 2, 2010 IP
    Skillman13 likes this.
  4. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks, that works perfectly :)

    Great...
     
    Skillman13, Jan 3, 2010 IP