Skip to content

Commit

Permalink
Merge pull request #58 from SWM-FIRE/FIRE-330-fe-방-시작하면-0-00-에서-시작하는-…
Browse files Browse the repository at this point in the history
…타이머-출력

FIRE-330-fe-방-시작하면-0-00-에서-시작하는-타이머-출력
  • Loading branch information
071yoon authored Jul 18, 2022
2 parents 0f71c64 + 5bde28b commit 2786de9
Showing 1 changed file with 42 additions and 2 deletions.
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;
`;

1 comment on commit 2786de9

@vercel
Copy link

@vercel vercel bot commented on 2786de9 Jul 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.