Skip to content Skip to sidebar Skip to footer

Jquery Ajax Call Function On Timeout

I am trying to reload my jQuery DataTables without refreshing the page in an attempt to capture new data. Here is my initial ready function that begins the process: $(document).re

Solution 1:

I think this example will be usefull

//Reload the table data every 30 seconds (paging reset)var table = $('#example').DataTable( {
    ajax: "data.json"
} );

setInterval( function () {
    table.ajax.reload();
}, 30000 );

more details - here

Solution 2:

Create a function to load/render data then call it in document ready and after 2 seconds:

functionloadAndRender()
{
    $.ajax(
    {
        url:'api/qnams_all.php',
        type:"GET",
        dataType:"json"
    }).done(function(response)
    {
        console.log(response.data);
        renderDataTable(response.data)
    }).fail(function()
    {
        alert( "error" );
    }).always(function()
    {
        alert( "complete" );
    });  

}

functionrenderDataTable(data)
{
    var $dataTable = $('#example1').DataTable(
    {
        "data": data,
        "iDisplayLength": 25,
        "order": [[ 6, "desc" ]],
        "bDestroy": true,
        "stateSave": true// there's some more stuff, but I don't think necessary to show
    });
}

$(document).ready(loadAndRender);

setTimeout(loadAndRender, 2000);

Post a Comment for "Jquery Ajax Call Function On Timeout"