-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1603 from ecency/feature/3speak-limitation
Feature/3speak limitation
- Loading branch information
Showing
5 changed files
with
135 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useCallback, useEffect, useRef, useState } from "react"; | ||
|
||
interface TimeState { | ||
hours: number; | ||
minutes: number; | ||
seconds: number; | ||
} | ||
|
||
export function useStopwatch() { | ||
const [time, setTime] = useState<TimeState>({ seconds: 0, minutes: 0, hours: 0 }); | ||
const [isActive, setIsActive] = useState(false); | ||
const intervalRef = useRef<any>(null); | ||
|
||
const tick = useCallback(() => { | ||
setTime((prevTime) => { | ||
let hours = prevTime.hours; | ||
let minutes = prevTime.minutes; | ||
let seconds = prevTime.seconds; | ||
|
||
seconds += 1; | ||
|
||
if (seconds >= 60) { | ||
minutes += 1; | ||
seconds = 0; | ||
} | ||
|
||
if (minutes >= 60) { | ||
hours += 1; | ||
minutes = 0; | ||
} | ||
|
||
return { hours, minutes, seconds }; | ||
}); | ||
}, []); | ||
|
||
const clear = useCallback(() => { | ||
clearInterval(intervalRef.current); | ||
intervalRef.current = null; | ||
setTime({ seconds: 0, minutes: 0, hours: 0 }); | ||
}, []); | ||
|
||
const start = useCallback(() => { | ||
setIsActive(true); | ||
intervalRef.current = setInterval(tick, 1000); | ||
}, [tick]); | ||
|
||
useEffect(() => { | ||
return () => { | ||
clear(); | ||
}; | ||
}, [clear]); | ||
|
||
return { | ||
...time, | ||
isActive, | ||
clear, | ||
start, | ||
setIsActive | ||
}; | ||
} |