简体   繁体   English

如何使用 react JS 将 5 分钟倒计时间隔计时器偏移 1 分钟

[英]How can I offset a 5 minute countdown interval timer by 1 minute, using react JS

I am looking to make a 5-minute interval timer in react JS, that's offset by 1 minute.我希望在 react JS 中制作一个 5 分钟的间隔计时器,偏移 1 分钟。
The timer I have below lands at 1:00 => 1:05 => 1:10 => 1:15 => 1:20.我下面的计时器降落在 1:00 => 1:05 => 1:10 => 1:15 => 1:20。
I need this to be offset to 1:01 => 1:06 => 1:11 => 1:16 => 1:21.我需要将其偏移到 1:01 => 1:06 => 1:11 => 1:16 => 1:21。
The timer needs to be in-sync with the Los Angeles time-zone.计时器需要与洛杉矶时区同步。
Looking for the cleanest es7 code if possible.如果可能,寻找最干净的 es7 代码。
I am using NextJS framework.我正在使用 NextJS 框架。
This is the code I have so far.这是我到目前为止的代码。

  const [timer, setTimer] = useState("")

  const secondPassed = useCallback( () => {
    const cur_date = new Date();
    const minutes = cur_date.getMinutes();
    const seconds = cur_date.getSeconds();
    console.log( (4 - minutes % 5) + ":" + (seconds >= 50 ? "0" : "") + (59 - seconds))
    setTimer( `${(4 - minutes % 5) + ":" + (seconds >= 50 ? "0" : "") + (59 - seconds)}` )
  },[])
  
  useEffect(()=>{
    setInterval(secondPassed, 1000)
  },[])

I offset the starting date by 1 minute.我将开始日期偏移了 1 分钟。

const [timer, setTimer] = useState("")

  const secondPassed = useCallback( () => {
    const cur_date = new Date( Date.now() - 1000 * 60 );
    const minutes = cur_date.getMinutes();
    const seconds = cur_date.getSeconds();
    console.log( (4 - (minutes % 5)) + ":" + (seconds >= 50 ? "0" : "") + (59 - seconds))
    setTimer( `${(4 - (minutes % 5)) + ":" + (seconds >= 50 ? "0" : "") + (59 - seconds)}` )
  },[])
  
  useEffect(()=>{
    const run = setInterval(secondPassed, 5000)
    return () => clearInterval(run)
  },[])
  
const [time, settime] = useState()

const cur_date = new Date();
const minutes = cur_date.getMinutes();
const seconds = cur_date.getSeconds();

if(minutes === "your timing start") {
settime(minutes) }

if(minutes ==== time + 5 ) {
settime(minutes)
}

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

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