04.19

조하빈 ·2023년 4월 19일
0

04월 19일 수요일


아기사자 스터디 STOPWATCH 만들기

시간 변수 설정하는게 가장 어려웠다

const hour = Math.floor(time / (3600 * 100));
  const min = Math.floor((time % 360000) / 6000);
  const sec = Math.floor((time % 6000) / 100);
  const msec = time % 100;

그리고 시간을 0:00:00:00 단위로 표현하는 것에서 애를 좀 먹었다..

{hour}:{min.toString().padStart(2, "0")}:
        {sec.toString().padStart(2, "0")}:{msec.toString().padStart(2, "0")}

git 주소 : https://github.com/PPisland/Stopwatch


  • 오늘 배운 것 :
    setInterval을 이용해 ms단위로 시간이 가게 하는 코드를 매니저님께 배웠다.. 아직 개념이 확실하게 잡힌건 아니나 Clear함수를 활용하는 법을 배워서 다음에 써먹을곳을 생각해봐야겠다.
  useEffect(() => {
    if (isLoading) {
      let interverId = setInterval(() => {
        setTime(time + 1);
      }, 10);

      return () => {
        clearInterval(interverId);
      };
    }
  }, [time, isLoading]);
  • 각종 팁 :
profile
PPisland

0개의 댓글