I want to make a call to an API once an hour, every hour, using setInterval, how would I go about implementing that?
componentDidMount() {
fetch(fullURL)
.then((response) => response.json())
.then((responseJson) => {
// console.log(responseJson);
const resultyt = responseJson.items.map(obj => "https://www.youtube.com/embed/" + obj.id.videoId);
this.setState({resultyt});
})
.catch((error) => {
console.error(error);
});
}
the API call is stored inside a const called fullURL
Wrap that up in a function that can be called many times, then simply use setInterval:
componentDidMount() {
this.intervalId = setInterval(() => this.loadData(), 3600000);
this.loadData(); // also load one immediately
}
componentWillUnmount() {
clearInterval(this.intervalId);
}
loadData() {
fetch(fullURL).then(...);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With