Toggle navigation
Toggle navigation
This project
Loading...
Sign in
강동현
/
nodejs-game
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Overnap
2021-06-10 07:54:00 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
652a9844eb4fe71568ff05ec6e4b3dc742689ec3
652a9844
1 parent
4debe593
Timer 컴포넌트 추가
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
0 deletions
web/src/components/room/Timer.tsx
web/src/components/room/Timer.tsx
0 → 100644
View file @
652a984
import React, { useCallback, useContext, useEffect, useState } from 'react';
import SocketContext from '../../contexts/SocketContext';
import { MessageType, RawMessage } from '../common/types';
interface timer {
state: "started" | "stopped";
time: number;
};
export const Timer: React.FC = () => {
const socket = useContext(SocketContext);
const [ time, setTime ] = useState(0);
const [ isStop, setIsStop ] = useState(true);
const handleTimeSet = useCallback((rawMessage: RawMessage) => {
if (rawMessage.type === MessageType.GAME_TIMER) {
const data = rawMessage.message as timer;
console.log(data);
if (data.state === 'started') {
setIsStop(false);
} else {
setIsStop(true);
}
setTime(Math.floor(data.time));
}
}, []);
useEffect(() => {
if (!isStop) {
const go = setInterval(() => {
setTime(time-1);
}, 1000);
return () => clearInterval(go);
} else {
setTime(0);
}
}, [time, isStop]);
useEffect(() => {
socket.on('msg', handleTimeSet);
return () => {
socket.off('msg', handleTimeSet);
}
}, []);
return (
<div className={time < 10 ? 'text-red-500' : 'text-black'}>
🕒 {time}
</div>
);
}
\ No newline at end of file
Please
register
or
login
to post a comment