How could we use a setState
inside setTimeout. I declared a property inside constructor function. I used this.setState({ count: index });
inside setTimeout
it throws an error like Uncaught TypeError: this.setState is not a function
constructor(props) {
super(props);
this.state = {
count: 0
};
this.onMouseDown = this.onMouseDown.bind(this);
}
onMouseDown() {
this.timer();
}
timer() {
for (var i = 1; i <= 90; i++) {
(function(index) {
setTimeout(function() {
this.setState({ count: index });
}, index * 10);
})(i);
}
}
I tried a lot don't know how to fix this. And why it is wrong?.Please get me out of this problem..