Created
March 20, 2018 08:17
-
-
Save shrynx/63451f0519c93a0944312a1cea998820 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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