0

I am keeping a live track on no. of users in my system, for that I am using javascript's setInterval() function, following is the code:

function fetchdis() {
$.ajax({
    url: "/count/dist",
    dataType: "json"
}).success(function(data){
    $('#val_dis').html(JSON.stringify(data.sum));
});
}
setInterval(function () {fetchdis()}, 3000);

everything is working properly except I am getting a delay of 3 seconds also first time when I am loading the data(resulting into an empty space), I do not want that interval the first time the data loads.

Thank you for your help

sameer manek
  • 735
  • 1
  • 11
  • 34

1 Answers1

1

That's because setInterval executes the callback every 3 seconds, and you haven't called the function anywhere else.

function fetchdis() {
  $.ajax({
      url: "/count/dist",
      dataType: "json"
  }).success(function(data){
      $('#val_dis').html(JSON.stringify(data.sum));
  });
}

fetchdis();
setInterval(function () {
  fetchdis();
}, 3000);

Notice that I'm calling fetchdis() right before I register the interval.

A.J.
  • 393
  • 1
  • 8