I want to make a call to an API once an hour, every hour, using setInterval, how would I go about implementing that?
ponentDidMount() {
fetch(fullURL)
.then((response) => response.json())
.then((responseJson) => {
// console.log(responseJson);
const resultyt = responseJson.items.map(obj => "/" + obj.id.videoId);
this.setState({resultyt});
})
.catch((error) => {
console.error(error);
});
}
the API call is stored inside a const called fullURL
I want to make a call to an API once an hour, every hour, using setInterval, how would I go about implementing that?
ponentDidMount() {
fetch(fullURL)
.then((response) => response.json())
.then((responseJson) => {
// console.log(responseJson);
const resultyt = responseJson.items.map(obj => "https://www.youtube./embed/" + obj.id.videoId);
this.setState({resultyt});
})
.catch((error) => {
console.error(error);
});
}
the API call is stored inside a const called fullURL
Share Improve this question edited Nov 19, 2017 at 17:24 linasmnew 3,9772 gold badges22 silver badges33 bronze badges asked Nov 19, 2017 at 16:40 Akil HyltonAkil Hylton 211 silver badge6 bronze badges1 Answer
Reset to default 9Wrap that up in a function that can be called many times, then simply use setInterval:
ponentDidMount() {
this.intervalId = setInterval(() => this.loadData(), 3600000);
this.loadData(); // also load one immediately
}
ponentWillUnmount() {
clearInterval(this.intervalId);
}
loadData() {
fetch(fullURL).then(...);
}