Skip to content Skip to sidebar Skip to footer

Jquery Get Data From Php Page After Every 2 Secounds

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

Solution 1:

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.


Solution 2:

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);

Solution 3:

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.
}

Solution 4:

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);

Post a Comment for "Jquery Get Data From Php Page After Every 2 Secounds"