Skip to content

Commit

Permalink
feat: use random interval
Browse files Browse the repository at this point in the history
  • Loading branch information
wkylin committed Dec 27, 2024
1 parent e029861 commit d7e4cf6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"tryer",
"urlparams"
],
"files.autoSave": "onWindowChange",
"files.autoSave": "off",
"npm-scripts.showStartNotification": false,
"editor.suggest.snippetsPreventQuickSuggestions": false,
"aiXcoder.showTrayIcon": true,
Expand Down
11 changes: 11 additions & 0 deletions src/assets/svg/noise.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions src/components/hooks/useRandomInterval/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react'

Check failure on line 1 in src/components/hooks/useRandomInterval/index.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

ESLint

ESLint: Install the 'eslint' package
// Utility helper for random number generation
const random = (min, max) => Math.floor(Math.random() * (max - min)) + min

const useRandomInterval = (callback, minDelay, maxDelay) => {

Check warning on line 5 in src/components/hooks/useRandomInterval/index.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Unused local symbol

Unused constant useRandomInterval
const timeoutId = React.useRef(null)
const savedCallback = React.useRef(callback)

React.useEffect(() => {
savedCallback.current = callback
}, [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 () {

Check warning on line 32 in src/components/hooks/useRandomInterval/index.jsx

View workflow job for this annotation

GitHub Actions / Qodana for JS

Redundant local variable

Local variable cancel is redundant
window.clearTimeout(timeoutId.current)
}, [])

return cancel
}

// const [value, setValue] = React.useState([400, 1000]);
// const [key, setKey] = React.useState(null);
// useRandomInterval(() => setKey(Math.random()), ...value);
12 changes: 12 additions & 0 deletions src/utils/publicFn/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,15 @@ export const openInNewTab = (url) => {
win.focus()
return win
}

export const range = (start, end, step = 1) => {
let output = []
if (typeof end === 'undefined') {
end = start
start = 0
}
for (let i = start; i < end; i += step) {
output.push(i)
}
return output
}

0 comments on commit d7e4cf6

Please sign in to comment.