mirror of
https://github.com/sr229/kuru-kuru.git
synced 2024-09-20 04:10:33 +00:00
Properly implement internal counter
Signed-off-by: GitHub <noreply@github.com>
This commit is contained in:
parent
77781faca9
commit
81931acd47
1 changed files with 10 additions and 4 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue