MCPcopy Create free account
hub / github.com/pgadmin-org/pgadmin4 / useStopwatch

Function useStopwatch

web/pgadmin/static/js/custom_hooks.js:119–162  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

117}
118
119export function useStopwatch() {
120 const prevTime = useRef(new Date());
121 const [totalMsec, setTotalMsec] = useState(0);
122 const [isRunning, setIsRunning] = useState(false);
123
124 useInterval(() => {
125 setTotalMsec(moment(new Date()).diff(prevTime.current));
126 }, isRunning ? 100 : -1);
127
128 function start(startTime) {
129 prevTime.current = startTime || new Date();
130 setIsRunning(true);
131 }
132
133 const pause = (endTime)=>{
134 setIsRunning(false);
135 setTotalMsec(moment(endTime || new Date()).diff(prevTime.current));
136 };
137
138 function reset() {
139 setTotalMsec(0);
140 }
141
142 let msec = totalMsec;
143 /* Extract seconds from millisecs */
144 let seconds = parseInt(msec/1000);
145 msec = msec%1000;
146
147 /* Extract mins from seconds */
148 let minutes = parseInt(seconds/60);
149 seconds = seconds%60;
150
151 /* Extract hrs from mins */
152 let hours = parseInt(minutes/60);
153 minutes = minutes%60;
154
155 return {
156 hours: hours,
157 minutes: minutes,
158 seconds: seconds,
159 msec: msec,
160 start, pause, reset, isRunning,
161 };
162}
163
164/*
165 shortcuts = [

Callers 1

StatusBarFunction · 0.90

Calls 1

useIntervalFunction · 0.85

Tested by

no test coverage detected