I cant find a way to refresh that table like avery 10 seconds ...

I cant find a way to refresh that table like avery 10 seconds ...

simondatassimondatas Posts: 4Questions: 3Answers: 0

Hello,
I have this code which works very well. Now I would like to refresh the datatable with my api every 10 seconds but is has been hours and I cant make it work ...
Any hep would be appreciated.
Many thanks
SImon

<script>
$(document).ready(function(){
    
    var myPath = window.location.href;
    console.log(myPath);
    var newDatas = null;
    var table = null;
    
    $.ajax({
    type: "GET",
    url: myPath+"/refresh",
    success: function(data) {
        console.log("wow");
        newDatas = JSON.parse(data);
        console.log(newDatas);
        table = $('#myTable').DataTable({
            
            data: newDatas,
            columns:[
                {data :  "lang"},
                {data : "source"},
                {data : "author.screen_name"},
                {data : "author.description"},
                {data : "full_text"},
                {data : "created_at"},
                {data : "author.location"},
                {data : "author.followers_count"},
                {data : "author.verified"},
                {data : "author.name"}


            ]
        });
        
    }
    })


});

</script>

Answers

  • simondatassimondatas Posts: 4Questions: 3Answers: 0

    This works :
    '''javascript
    $(document).ready(function(){

    var myPath = window.location.href;
    console.log(myPath);
    var newDatas = null;
    var table = null;
    
    table = $('#myTable').DataTable({
        "ajax": {
            "type": "GET",
            "url": myPath+"/refresh",
            "dataSrc": function(data){
                //console.log(data);
                newDatas=data;
                //console.log(newDatas);
                return newDatas;
    
            },
    
        },
    columns:[
        {data :  "lang"},
        {data : "source"},
        {data : "author.screen_name"},
        {data : "author.description"},
        {data : "full_text"},
        {data : "created_at"},
        {data : "author.location"},
        {data : "author.followers_count"},
        {data : "author.verified"},
        {data : "author.name"}
    
    
    ]
    
    });
    setInterval( function () {
        table.ajax.reload( null, false ); // user paging is not reset on reload
    }, 10000 );
    

    });
    '''

  • allanallan Posts: 63,338Questions: 1Answers: 10,438 Site admin

    Yup - a setInterval which calls ajax.reload() would be the way to do this.

    Allan

This discussion has been closed.