How to implement polling in javascript

1.3k Views Asked by At

I want to get data from the server every one second. Suppose that I have data.json in the server. I want to fetch that file. Can I use the following code.

function getData(){
    fetch('./data.json')
        .then((result) => console.log(result))
        .then(() => setTimeout(getData,1000))
        .catch((err) => console.log(err));
}

Does this method cause a stackoverflow. Cant I use fetch or do I need to use ajax. Is there a better way to implement this/

0

There are 0 best solutions below