简体   繁体   中英

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

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM