Skip to content

Instantly share code, notes, and snippets.

@shrynx
Created March 20, 2018 08:17
Show Gist options
  • Save shrynx/63451f0519c93a0944312a1cea998820 to your computer and use it in GitHub Desktop.
Save shrynx/63451f0519c93a0944312a1cea998820 to your computer and use it in GitHub Desktop.
state = {
everySecond: 0,
every5Second: 0,
random: 0,
};
incrementEverySecond = () => {
this.setState(({ everySecond }) => ({ everySecond: everySecond + 1 }));
};
incrementEvery5Second = () => {
this.setState(({ every5Second }) => ({ every5Second: every5Second + 1 }));
};
incrementRandom = () => {
this.setState(({ random }) => ({ random: random + 1 }));
};
componentDidMount() {
setInterval(this.incrementEverySecond, 1000);
setInterval(this.incrementEvery5Second, 5000);
{
const randomIntFromRange = (min, max) =>
Math.trunc(Math.random() * (max - min + 1) + min);
const randomInterval = () => {
setTimeout(() => {
this.incrementRandom();
randomInterval();
}, randomIntFromRange(100, 500));
};
randomInterval();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment