Create simple countdown timer

This commit is contained in:
Ray 2024-01-04 15:21:40 +11:00
parent 22674197a3
commit 37bbc1472f
1 changed files with 36 additions and 0 deletions

36
src/Timer.js Normal file
View File

@ -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 (
<div className="timer">
</div>
);
};
export default Timer;