0

I am get data from php page after every 2 secounds the data is very large when i call it once then the data comes but when i place my code in setinterval function then the data in console is not showing I place this code in setinterval function because after every 2 sec i need fresh data any idea plz share

var data_array = '';

     setInterval(function () {
        $.ajax({
            url:"./phponline.php",
            async:false,
            success:function(res)
            {
                data_array = res;
            },
            error:function(errorMsg)
            {

            }
        }); 
         }, 5000);
 console.log(data_array); 
Waqas Khan
  • 257
  • 2
  • 3
  • 16
  • Put `console.log(data_array);` inside the success handler. – Phylogenesis Nov 10 '15 at 09:50
  • i need the data_array out side function @Phylogenesis – Waqas Khan Nov 10 '15 at 09:51
  • but your console.log(data_array); is not a part of set interval function – Muhammad Atif Nov 10 '15 at 09:52
  • @MuhammadAtif i declare the data_array variable globaly so it must be accesable out side function – Waqas Khan Nov 10 '15 at 09:54
  • 2
    i know that but in your current code, console.log(data_array); will run only first time as it is not part of setInterval function – Muhammad Atif Nov 10 '15 at 09:57
  • success:function(res) { data_array = res; console.log(data_array); }, – Muhammad Atif Nov 10 '15 at 09:59
  • 1
    Others have answered the issue you've had with the data seeming to not be updated, but there's another issue that you need to deal with. Using `setInterval` could cause you to be making multiple requests at the same time. If for any reason the ajax call takes longer than 2 seconds (or 5 in your code) then you'll make another request while one is still active. This will compound over time and make the page unresponsive. Place the ajax call inside a function and use `setTimeout` to call the function inside the ajax success handler, so it gets data and then calls the function again *after*. – Reinstate Monica Cellio Nov 10 '15 at 10:07
  • You are attempting to read `data_array` before it has been set in your AJAX call. You are trying to subvert causality. – Phylogenesis Nov 10 '15 at 10:10

4 Answers4

0

Your console.log should be called in the success callback or directly after the ajax call in the setInterval callback.

If you place the console.log after the setInterval, data_array will be empty because it's setted 5 seconds later.

 var data_array = '';

 setInterval(function () {
    $.ajax({
        url:"./phponline.php",
        async:false,
        success:function(res)
        {
            data_array = res;
            console.log(data_array); 
        },
        error:function(errorMsg)
        {

        }
    }); 
 }, 5000);
0

As Muhammed Atif said in the comments, you need to place the console log inside the SetInterval function.

var data_array = '';

function handleData() {
    console.log(data_array);
}

setInterval(function () {
    $.ajax({
        url:"./phponline.php",
        async:false,
        success:function(res)
        {
            data_array = res;
            handleData();
        },
        error:function(errorMsg)
        {

        }
    }); 
}, 5000);
Timon
  • 2,682
  • 1
  • 20
  • 33
0

you need to call some custom function inside ajax success of setInterval to provide the effect of response stored in data_array:

var data_array = '';
    $(document).on('ready', function(event, data) {


        setInterval(function () {
           $.ajax({
               url:"./phponline.php",
               async:false,
               success:function(res)
               {
                   data_array = res;
                   updateWithData();
               },
               error:function(errorMsg)
               {

               }
           }); 

            }, 5000);
     updateWithData();
    });

function updateWithData(){
//use data_array to make changes on each call.
}
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
0

There's a couple of issues you have here, the main one being that you're trying to make a synchronous ajax call, and that's been deprecated. You need to handle it being an asynchronous call instead...

Put the code that you want to run every time you get new data into a function and call that function in the success callback

var data_array = '';  // this is a global variable

function getNewData() {
    $.ajax({
        url: "./phponline.php",
    })
    .done(function(res) {
        data_array = res;  // the global variable is updated here and accessible elsewhere
        getNewDataSuccess();
    })
    .fail(function() {
        // handle errors here
    })
    .always(function() {
        // we've completed the call and updated the global variable, so set a timeout to make the call again
        setTimeout(getNewData, 2000);
    });
}

function getNewDataSuccess() {
    console.log(data_array); 
}

getNewData();

As I explained in the comments, using setInterval with an ajax call is a bad idea as you could end up overlapping calls if they take longer than the interval. This method makes a call, waits for the result and then uses setTimeout to make the next call instead. This way you can never make an ajax call until 2 seconds after the last one was completed.

Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67