jack jack - 14 days ago 5
jQuery Question

JQuery setTimeout doesn't work for some reason

I'm still new to JQuery, I used setTimeout to load the inserted data after certain time. I tried the examples as well as the answers about this issue but nothing worked!

What I'm doing wrong?

<script>
$(document).ready(function(){
function fetch_data(){

$.ajax({
url:"getdata.php",
success: function(data){
$('.result').html(data);
}

});
}
fetch_data();

setTimeout(function() {
fetch_data();
}, 2000);


});
</script>

Answer

Just use setInterval. Here's the code you will need. Hope it helps!

JAVASCRIPT

 $(document).ready(function(){
    function fetch_data(){

        $.ajax({
            url:"getdata.php",
            success: function(data){
                $('.result').html(data);
            }

        });
    }
    fetch_data();

    setInterval(function() {
        fetch_data();
    }, 2000);
});