From 37bbc1472fd7c8ddfd8877e912b68c5b82fe6084 Mon Sep 17 00:00:00 2001 From: Reiyua Date: Thu, 4 Jan 2024 15:21:40 +1100 Subject: [PATCH] Create simple countdown timer --- src/Timer.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/Timer.js diff --git a/src/Timer.js b/src/Timer.js new file mode 100644 index 0000000..ba6f378 --- /dev/null +++ b/src/Timer.js @@ -0,0 +1,36 @@ +// Timer.js + +import React from 'react'; +import { useState } from 'react'; + +const Timer = () => { + const [days, setDays] = useState(0); + const [hours, setHours] = useState(0); + const [minutes, setMinutes] = useState(0); + const [seconds, setSeconds] = useState(0); + + // Assign a deadline for counter to count down to, in full month/day/year format. + const deadline = "December, 31, 2022"; + + const getTime = () => { + const time = Date.parse(deadline) - Date.now(); + + setDays(Math.floor(time / (1000 * 60 * 60 * 24))); + setHours(Math.floor((time / (1000 * 60 * 60)) % 24)); + setMinutes(Math.floor((time / 1000 / 60) % 60)); + setSeconds(Math.floor((time / 1000) % 60)); + }; + + React.useEffect(() => { + const interval = setInterval(() => getTime(deadline), 1000); + + return () => clearInterval(interval); + }, []); + + return ( +
+
+ ); +}; + +export default Timer; \ No newline at end of file