0

How can I execute this function immediately, then start the setInterval timer?

Here is my js.

$(document).ready(function(){
        var callAjax = function(){
          $.ajax({
            method:'get',
            url:'apinvs.php',
            dataType:'html',        
            success:function(data){
              $("#main").html(data); } 
          });
        }
                setInterval(callAjax,2500); 
      });

It's executing the function after 2.5 seconds. It leaves the div blank until 2.5 seconds pass, which is what I don't want to happen. :(

I found a Stackoverflow question here but I'm not sure how to apply it with my code (it's using document ready). Thanks for any assistance.

Community
  • 1
  • 1
llw
  • 201
  • 1
  • 3
  • 15

3 Answers3

1

Change

setInterval(callAjax,2500); 

to

callAjax();
setInterval(callAjax,2500); 
Mitya
  • 33,629
  • 9
  • 60
  • 107
  • 1
    Thanks. I just posted an answer (I figured it out myself, can't believe it was so simple). Spot on! – llw Oct 13 '14 at 22:30
0

do you mean something like this? or do I get your question wrong?

$(document).ready(function(){
    var callAjax = function(){
      $.ajax({
        method:'get',
        url:'apinvs.php',
        dataType:'html',        
        success:function(data){
          $("#main").html(data);
          setInterval(callAjax,2500); 
        }
      });
    }
  });
0

Nevermind, can't believe it was this simple.

Changed my code to..

$(document).ready(function(){
        var callAjax = function(){
          $.ajax({
            method:'get',
            url:'apinvs.php',
            dataType:'html',        
            success:function(data){
              $("#main").html(data); } 
          });
        }
                setInterval(callAjax,5000); 
                callAjax();
      });

(called the function)

It's working perfectly now.

llw
  • 201
  • 1
  • 3
  • 15