-1

I'm trying to poll a NodeJs Server using the fetch function but the setInterval method is actually not working as expected (it runs only once and then stops).

window.onload = () => {
    let label = document.getElementById('button_badge');
    setInterval(getNotification(label), 3000);
};

function getNotification(label) {
    fetch('/getnotification')
        .then(value => {
            value.json()
                .then(value1 => {
                    console.log(value1);
                    label.innerHTML = value1.listWatchers.length;
                });
        })
        .catch(reason => {
            console.log(reason)
        });
}

The final result (which is value1 in this case) should be used to set a span value (label variable)

Andrea
  • 134
  • 2
  • 12

1 Answers1

1

The setInterval function accepts a function as the first parameter. In your code you are not passing the function as a parameter, but rather calling the function instead.

Change it to this:

setInterval(function(){
    getNotification(label)
}, 3000);
Jackson
  • 3,476
  • 1
  • 19
  • 29