Properly implement internal counter

Signed-off-by: GitHub <noreply@github.com>
This commit is contained in:
Ayase Minori 2023-09-11 04:00:53 +00:00 committed by GitHub
parent 77781faca9
commit 81931acd47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,25 +10,31 @@ interface SharedProps {
export default function Counter(props: SharedProps) {
const [count, setCount] = useState(0);
const [internalCount, setInternalCount] = useState(0)
const onClick = () => {
let internalCount = 0
let timer: number;
// set a timer to update the global count, resetting
// whenever a user activity is detected
internalCount += 1;
setInternalCount(internalCount + 1)
setCount(count + 1);
timer = setTimeout(() => {
console.info(`[${new Date()}] Updating global count: ${internalCount}`);
axios.post(window.location.href, JSON.stringify({data: internalCount}));
internalCount = 0;
setInternalCount(0);
}, 5000);
window.onclick = () => {
clearTimeout(timer);
timer = setTimeout(() => {
console.info(`[${new Date()}] Updating global count: ${internalCount}`);
axios.post(window.location.href, JSON.stringify({data: internalCount}));
internalCount = 0;
setInternalCount(0);
}, 5000);
}
}