简体   繁体   English

Day.js 倒数计时器

[英]Day.js countdown timer

I need to create a simple timer that is counting from 10 minutes down and after 10 minuets it will chnage isValid from true to false .我需要创建一个简单的计时器,它从 10 分钟开始计数,10 分钟后它会将isValidtrue更改为false But I was ask to do this with Day.js.但是我被要求用 Day.js 来做这件事。 This is getting a bit tricky.这变得有点棘手。 I am checking Day.js docs but I am faling to get this working.我正在检查 Day.js 文档,但我无法使其正常工作。 This is waht i got, nut much and not even close to get this working properly:这就是我得到的,非常疯狂,甚至还没有接近使其正常工作:

const [minutes, setMinutes] = useState(0);
const [seconds, setSeconds] = useState(0);
const tokenTTL = 10;
// let difference;
const now = dayjs().second();
// const nowFormat = dayjs().format("hh:mm:ss");
const addTime = dayjs().add(tokenTTL, "minute");
const timeTo = addTime.subtract(now).minute();

useEffect(() => {
    // const actualTime = new Date();
    // const target = new Date(
    //   actualTime.setMinutes(actualTime.getMinutes() + tokenTTL)
    // );

    const interval = setInterval(() => {
        console.log(now);
        console.log("TT", timeTo);
        console.log("add", addTime.format("mm:ss"));
        console.log("nf", dayjs().second());
        console.log(TimeHelper.getCurrentDateTime());

        setMinutes(dayjs().minute());
        setSeconds(dayjs().second());
        // const now = new Date();
        // difference = target.getTime() - now.getTime();
        // let m: string | number = Math.floor(
        //   (difference % (1000 * 60 * 60)) / (1000 * 60)
        // );
        // m = m < 10 ? "0" + m : m;
        // setMinutes(m as number);
        // let s: string | number = Math.floor((difference % (1000 * 60)) / 1000);
        // s = s < 10 ? "0" + s : s;
        // setSeconds(s as number);
        // if (difference === 0) {
        //   isValid(false);
        // }
    }, 1000);
    return () => clearInterval(interval);
}, []);

return (
    <p className="Timer">
        <span className="minutes">{minutes}</span>
        <span className="divider">:</span>
        <span className="seconds">{seconds}</span>
    </p>
);

As you can see, with some basic stuff that tehy provide I do get minutes and seconds using setMinutes(dayjs().minute());正如你所看到的,使用setMinutes(dayjs().minute());提供的一些基本内容,我确实使用setMinutes(dayjs().minute());获得了分钟和秒setMinutes(dayjs().minute()); and setSeconds(dayjs().second());setSeconds(dayjs().second()); also i was able to add 10 minutes to my current time that I am getting from const now = dayjs().second();我还能够将我从const now = dayjs().second();获得的当前时间增加 10 分钟const now = dayjs().second(); const addTime = dayjs().add(tokenTTL, "minute"); but this is all i can get from this.但这就是我能从中得到的全部。 I am strugling to get this properly worknig and counting down from 10 minutes.我正在努力使其正常工作并从 10 分钟开始倒计时。

Is this even posible to get this done with Day.js?这甚至可以用 Day.js 完成吗?

This is my timer component:这是我的计时器组件:

    import React,{ useEffect, useState } from "react";
export default function Timer() {
  const [time, setTime] = useState();
  useEffect(() => {
    let duration = 2;
    let endTime = new Date();
    endTime.setMinutes(endTime.getMinutes() + duration);
    let min = duration;
    let sec = 0;
    
    let interval = setInterval(() => {
      sec--;
      if (sec === -1) {
        sec = 59;
        min--;
      }
      let newValue=<><span className="minutes">{formatNumber(min)}</span>
      <span className="divider">:</span>
      <span className="seconds">{formatNumber(sec)}</span></>
      if ((min === 0) && (sec === 0)) {
        clearInterval(interval);
      }
      setTime(newValue);      
    }, 1000);
  }, []);
  let formatNumber=(num)=>{
    return num.toLocaleString("en-US", {
      minimumIntegerDigits: 2,
      useGrouping: false
    });
  }
  return <p className="Timer">{time}</p>;
}

The demo URL: https://stackblitz.com/edit/react-cagrm7?file=src%2FTimer.js演示网址: https : //stackblitz.com/edit/react-cagrm7?file=src%2FTimer.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM