Files
traque/traque-app/hook/useTimeDifference.jsx
Sebastien Riviere a7f047388f Ajout traque-app
2025-08-24 10:32:57 +02:00

22 lines
616 B
JavaScript

import { useEffect, useState } from "react";
export function useTimeDifference(refTime, timeout) {
// If refTime is in the past, time will be positive
// If refTime is in the future, time will be negative
// The time is updated every timeout milliseconds
const [time, setTime] = useState(0);
useEffect(() => {
const updateTime = () => {
setTime(Math.floor((Date.now() - refTime) / 1000));
};
updateTime();
const interval = setInterval(updateTime, timeout);
return () => clearInterval(interval);
}, [refTime]);
return [time];
}