I am using 3 dynamic variable inside setInterval, in state
this.state={
checked: true,
currentHash:"some current hash",
updating: true
}
inside componentDidMount, i've done something like this
componentDidMount = () => {
let timer
timer = setInterval( (checked, currentHash, updating) => {
try {
this.setState({analysis:true});
if(checked){
var generatedHash = "current generated hash";
if (currentHash !== generatedHash) {
currentHash = generatedHash;
if(updating){
this.setState({updating:false})
const updateResponse = this.props.sendFile(DOCUMENT_ANALYSIS, ""); // my api call
}
else{
this.setState({analysis:false})
}
}
else{
this.setState({analysis:false})
}
clearInterval(timer);
this.componentDidMount();
}
else{
clearInterval(timer);
this.setState({analysis:false});
this.componentDidMount();
}
}
catch (error) {
console.log("Event error", error);
}
}, 10000, this.state.checked, this.state.currentHash, this.state.updating)
}
The interval is set to 10 seconds. But before getting correct state data, it is calling the same function twice.