AJAX loading interval help...

Discussion in 'JavaScript' started by Skillman13, Mar 25, 2010.

  1. #1
    I have some AJAX, -which will load the required page every 5000ms...
    However, on page load, it waits 5000ms to first load it,
    Does anyone know a solution to make it load, -on load... and then every 5000ms after?

    That would be great help...

    Thanks alot,

    James



    Code so far:

    <script type="text/javascript">
    function Ajax3(){
    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()
    {
    if(xmlHttp.readyState == 4)
    {
    document.getElementById('ReloadTime3').innerHTML=xmlHttp.responseText;
    }
    }
    xmlHttp.open("GET","live.php",true);
    xmlHttp.send(null);
    }
    window.onload=function(){
    setInterval('Ajax3()', 5000);
    }
    </script>
     
    Skillman13, Mar 25, 2010 IP
  2. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Call the functions once in the onload event and then enable your timer to reload it every 5 sec ;)
     
    Nyu, Mar 25, 2010 IP
  3. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Know how? :)
     
    Skillman13, Mar 25, 2010 IP
  4. mnvlxxx

    mnvlxxx Peon

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Put this inside the HEAD tag:
    
    <script type="text/javascript">
    function Ajax3(){
    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()
    {
    if(xmlHttp.readyState == 4)
    {
    document.getElementById('ReloadTime3').innerHTML=xmlHttp.responseText;
    }
    }
    xmlHttp.open("GET","live.php",true);
    xmlHttp.send(null);
    }
    </script> 
    
    Code (markup):
    Add this on the BODY tag of the page:
    
    <body onload="setInterval('Ajax3()', 5000);">
    
    Code (markup):
    Hope it helps.
     
    mnvlxxx, Mar 25, 2010 IP
  5. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Erm, no sorry, that doesn't work sorry, -It just loads it after 5000ms, -Like before :(

    Anyone else know how to do it? :)

    Thanks,

    James...
     
    Last edited: Mar 26, 2010
    Skillman13, Mar 26, 2010 IP
  6. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This should work.

    Put this Javascript in your head section:
    <script type="text/javascript">
    function Ajax3(){
    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()
    {
    if(xmlHttp.readyState == 4)
    {
    document.getElementById('ReloadTime3').innerHTML=xmlHttp.responseText;
    }
    }
    xmlHttp.open("GET","live.php",true);
    xmlHttp.send(null); 
    }
    //this will call your function every 5000ms
    [COLOR="red"]t=setInterval('Ajax3()', 5000);[/COLOR]
    }
    </script>
    Code (markup):
    Put this in your body tag:
    //this should call your function when the body loads
    <[COLOR="red"]body onload="Ajax3();">[/COLOR]
    Code (markup):

    Tell me if you have anymore problems.

    ~imozeb :)
     
    Imozeb, Mar 27, 2010 IP
  7. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Nope, sorry it's not loading at all...
    =/

    Anyone got an idea?
     
    Skillman13, Apr 4, 2010 IP
  8. hdewantara

    hdewantara Well-Known Member

    Messages:
    540
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #8
    Try 1st call ajax3() then setinterval() it.
    ...
    window.onload=function(){
      Ajax3();
      setInterval('Ajax3()', 5000);
    }
    </script>
    ...
    
    PHP:
     
    hdewantara, Apr 11, 2010 IP