fix: request timer issue (#3175)

This commit is contained in:
lohit 2024-09-23 16:01:17 +05:30 committed by GitHub
parent da9f669a2d
commit 6e4d7a6f76
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,25 +1,23 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
const StopWatch = ({ requestTimestamp }) => { const StopWatch = () => {
const [milliseconds, setMilliseconds] = useState(0); const [milliseconds, setMilliseconds] = useState(0);
const tickInterval = 200; const tickInterval = 100;
const tick = () => { const tick = () => {
setMilliseconds(milliseconds + tickInterval); setMilliseconds(_milliseconds => _milliseconds + tickInterval);
}; };
useEffect(() => { useEffect(() => {
let timerID = setInterval(() => tick(), tickInterval); let timerID = setInterval(() => {
tick()
}, tickInterval);
return () => { return () => {
clearInterval(timerID); clearTimeout(timerID);
}; };
}); }, []);
useEffect(() => { if (milliseconds < 250) {
setMilliseconds(Date.now() - requestTimestamp);
}, [requestTimestamp]);
if (milliseconds < 1000) {
return 'Loading...'; return 'Loading...';
} }
@ -27,4 +25,4 @@ const StopWatch = ({ requestTimestamp }) => {
return <span>{seconds.toFixed(1)}s</span>; return <span>{seconds.toFixed(1)}s</span>;
}; };
export default StopWatch; export default React.memo(StopWatch);