I'm working on a video player in ReactJS. Basically, I have set an intro video based on cookies, and it is skippable. When it is skipped, I want my main video to start playing automatically. The way I'm doing it looks like this (the problem lies in the setTimeout() part, I know it makes no sense)
skipIntro() {
if (this.state.index === 0) {
this.handleVisit(MAX_COOKIES_INTRO);
this.setState({
index: ++this.state.index,
});
this.videoRef.updatePlaybackTime(0);
setTimeout(() => {
this.videoRef.play();
}, 0);
}
}
If I don't use setTimeout, then this.videoRef.play();
does not execute. At first I thought it was because it was being asynchronously called before the main video had time to load. Since I'm waiting 0ms though, I'm very confused. Why does this work? I would really prefer to do this without a setTimeout call.