mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
tweak(ui/callTimer): resolved timer issue
This commit is contained in:
1 parent
10a8d792d8
commit
80e43f6f64
2 files changed
+29
-23
No files matched your search
@@ -1,36 +1,42 @@
|
|||||||
import React, { useCallback, useEffect, useRef } from 'react';
|
import React, { useEffect, useRef } from 'react';
|
||||||
import { useDuration } from '../hooks/useDuration';
|
import { useDuration } from '../hooks/useDuration';
|
||||||
|
|
||||||
|
|
||||||
|
const runTimer = (updatedTime, setDuration) => {
|
||||||
|
let newTime = { ...updatedTime.current };
|
||||||
|
if (newTime.m >= 60) {
|
||||||
|
newTime = { ...newTime, h: newTime.h + 1, m: 0 };
|
||||||
|
}
|
||||||
|
if (newTime.s >= 60) {
|
||||||
|
newTime = { ...newTime, m: newTime.m + 1, s: 0 };
|
||||||
|
}
|
||||||
|
if (newTime.ms >= 100) {
|
||||||
|
newTime = { ...newTime, s: newTime.s + 1, ms: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
newTime = { ...newTime, ms: newTime.ms + 1 };
|
||||||
|
updatedTime.current = newTime;
|
||||||
|
setDuration(newTime);
|
||||||
|
}
|
||||||
|
|
||||||
function CallTimer({ isAccepted }) {
|
function CallTimer({ isAccepted }) {
|
||||||
const { duration, setDuration } = useDuration();
|
const { duration, setDuration } = useDuration();
|
||||||
const updatedTime = useRef({ ms: 0, s: 0, m: 0, h: 0 });
|
const updatedTime = useRef({ ms: 0, s: 0, m: 0, h: 0 });
|
||||||
|
const timer = useRef(null)
|
||||||
|
|
||||||
const run = useCallback(() => {
|
|
||||||
let newTime = { ...updatedTime.current };
|
|
||||||
if (newTime.m >= 60) {
|
|
||||||
newTime = { ...newTime, h: newTime.h + 1, m: 0 };
|
|
||||||
}
|
|
||||||
if (newTime.s >= 60) {
|
|
||||||
newTime = { ...newTime, m: newTime.m + 1, s: 0 };
|
|
||||||
}
|
|
||||||
if (newTime.ms >= 100) {
|
|
||||||
newTime = { ...newTime, s: newTime.s + 1, ms: 0 };
|
|
||||||
}
|
|
||||||
|
|
||||||
newTime = { ...newTime, ms: newTime.ms + 1 };
|
|
||||||
updatedTime.current = newTime;
|
|
||||||
setDuration(newTime);
|
|
||||||
}, [setDuration]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let timer;
|
|
||||||
if (isAccepted) {
|
if (isAccepted) {
|
||||||
run();
|
runTimer(updatedTime, setDuration)
|
||||||
timer = setInterval(run, 10);
|
timer.current = setInterval(() => {
|
||||||
|
runTimer(updatedTime, setDuration)
|
||||||
|
}, 10);
|
||||||
} else {
|
} else {
|
||||||
clearInterval(timer);
|
clearInterval(timer.current);
|
||||||
|
timer.current = null;
|
||||||
}
|
}
|
||||||
}, [isAccepted, run]);
|
return () => clearInterval(timer.current)
|
||||||
|
}, [isAccepted, setDuration]);
|
||||||
|
|
||||||
if (!duration) return null;
|
if (!duration) return null;
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ export const callerState = {
|
|||||||
}),
|
}),
|
||||||
callModal: atom({
|
callModal: atom({
|
||||||
key: 'callingModal',
|
key: 'callingModal',
|
||||||
default: false
|
default: true
|
||||||
}),
|
}),
|
||||||
callDuration: atom({
|
callDuration: atom({
|
||||||
key: 'callDuration',
|
key: 'callDuration',
|
||||||
|
|||||||
Reference in new issue
Block a user