MCPcopy Create free account
hub / github.com/CammyLeone/devious-buttplug / useRandomInterval

Function useRandomInterval

goon-client/src/useRandomInterval.js:4–29  ·  view source on GitHub ↗
(callback, minDelay, maxDelay)

Source from the content-addressed store, hash-verified

2// Utility helper for random number generation
3const random = (min, max) => Math.floor(Math.random() * (max - min)) + min;
4const useRandomInterval = (callback, minDelay, maxDelay) => {
5 const timeoutId = React.useRef(null);
6 const savedCallback = React.useRef(callback);
7 React.useEffect(() => {
8 savedCallback.current = callback;
9 });
10 React.useEffect(() => {
11 let isEnabled =
12 typeof minDelay === "number" && typeof maxDelay === "number";
13 if (isEnabled) {
14 const handleTick = () => {
15 const nextTickAt = random(minDelay, maxDelay);
16 timeoutId.current = window.setTimeout(() => {
17 savedCallback.current();
18 handleTick();
19 }, nextTickAt);
20 };
21 handleTick();
22 }
23 return () => window.clearTimeout(timeoutId.current);
24 }, [minDelay, maxDelay]);
25 const cancel = React.useCallback(function () {
26 window.clearTimeout(timeoutId.current);
27 }, []);
28 return cancel;
29};
30export default useRandomInterval;

Callers 1

RotatingImages.jsFile · 0.85

Calls 1

handleTickFunction · 0.85

Tested by

no test coverage detected