Skip to content

Instantly share code, notes, and snippets.

@etienne-dldc
Created January 20, 2020 17:27
Show Gist options
  • Save etienne-dldc/8007175783669198ee258415ca2759c2 to your computer and use it in GitHub Desktop.
Save etienne-dldc/8007175783669198ee258415ca2759c2 to your computer and use it in GitHub Desktop.
const [start] = useState(Date.now());
const [, setRandomNum] = useState(0);
const diff = Date.now() - start;
useEffect(() => {
const current = diff % 1000;
const time = 1000 - current;
const timer = setTimeout(() => {
setRandomNum(Math.random());
}, time);
return () => {
clearTimeout(timer);
};
});
const num = 5 - Math.floor(diff / 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment