Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIRE-330-fe-방-시작하면-0-00-에서-시작하는-타이머-출력 #58

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/components/room/Timer.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,47 @@
import { useEffect, useState } from 'react';
import styled from 'styled-components';
import { ReactComponent as Divide } from '../../assets/svg/Divide.svg';

export default function Timer() {
const [time, setTime] = useState(0);
function timer() {
let start = new Date().getTime();
const callback = () => {
const ts = new Date().getTime();
if (ts - 1000 > start) {
setTime((time) => time + 1);
start = new Date().getTime();
requestAnimationFrame(callback);
} else {
requestAnimationFrame(callback);
}
};
requestAnimationFrame(callback);
}
useEffect(() => {
timer();
}, []);

const TimerHour =
Math.floor(time / 3600) < 10
? `0${Math.floor(time / 3600)}`
: Math.floor(time / 3600);

const TimerMinute =
Math.floor((time % 3600) / 60) < 10
? `0${Math.floor((time % 3600) / 60)}`
: Math.floor((time % 3600) / 60);

const TimerSecond =
Math.floor(time % 60) < 10
? `0${Math.floor(time % 60)}`
: Math.floor(time % 60);

return (
<Component>
<Status />
<Hour>03</Hour> : <Minute>17</Minute>
<Hour>{TimerHour}</Hour> :<Minute>{TimerMinute}</Minute> :
<Second>{TimerSecond}</Second>
<Divide />
</Component>
);
Expand All @@ -27,9 +63,13 @@ const Status = styled.div`
`;

const Hour = styled.span`
margin-right: 0.3rem; ;
margin-right: 0.3rem;
`;

const Minute = styled.span`
margin: 0 0.3rem 0 0.3rem;
`;

const Second = styled.span`
margin: 0 2.2rem 0 0.3rem;
`;