site/lib/use-random-interval.js
Lachlan Campbell dfdd2a9c91 Launch Elon announcement (#17)
* Begin Elon page

* Get content on Elon page

* Add content

* First pass

* Small change

* Light second pass

* Round 3 updates

* Small wording change

* Do another pass

* Remove biography paragraph

* Grammar

* Wording

* Break out sentence into own paragraph

* Grammar

* More small changes

* Fix small issue

* More small adjustments

* Final small changes for this pass

* Link Hack Club Bank

* Add preface

* Clarify that I had skillz

* Add frank.ly

* Clarify how money is spent in Elon text

* Add social card to Elon page

* Add Zach's signature

* Begin Elon page

* Get content on Elon page

* Add content

* First pass

* Small change

* Light second pass

* Round 3 updates

* Small wording change

* Do another pass

* Remove biography paragraph

* Grammar

* Wording

* Break out sentence into own paragraph

* Grammar

* More small changes

* Fix small issue

* More small adjustments

* Final small changes for this pass

* Link Hack Club Bank

* Add preface

* Clarify that I had skillz

* Add frank.ly

* Clarify how money is spent in Elon text

* Add social card to Elon page

* Add Zach's signature

* Update to latest

* Add my name under signature

* Upgrade dependencies

* Add sparkles

* Continue editing Elon page

* Improve badges

* Rebase

* Update card

* Pass of latest copy

* Small wording improvements

* Switch to @hackclub/meta 1.0.0

* Improve readability

* Add homepage announcement

* Fix meta tags on Elon page

Co-authored-by: Zach Latta <zach@zachlatta.com>
Co-authored-by: Christina Asquith <60162904+christinaasquith@users.noreply.github.com>
2020-05-20 12:48:52 -04:00

33 lines
1 KiB
JavaScript

// Full credit to https://joshwcomeau.com/snippets/react-hooks/use-random-interval
import React from 'react'
// Utility helper for random number generation
const random = (min, max) => Math.floor(Math.random() * (max - min)) + min
const useRandomInterval = (callback, minDelay, maxDelay) => {
const timeoutId = React.useRef(null)
const savedCallback = React.useRef(callback)
React.useEffect(() => {
savedCallback.current = callback
})
React.useEffect(() => {
let isEnabled = typeof minDelay === 'number' && typeof maxDelay === 'number'
if (isEnabled) {
const handleTick = () => {
const nextTickAt = random(minDelay, maxDelay)
timeoutId.current = window.setTimeout(() => {
savedCallback.current()
handleTick()
}, nextTickAt)
}
handleTick()
}
return () => window.clearTimeout(timeoutId.current)
}, [minDelay, maxDelay])
const cancel = React.useCallback(function () {
window.clearTimeout(timeoutId.current)
}, [])
return cancel
}
export default useRandomInterval