Javascript 在使用挂钩制作的计时器上格式化时间时遇到的问题

Javascript 在使用挂钩制作的计时器上格式化时间时遇到的问题,javascript,reactjs,react-hooks,Javascript,Reactjs,React Hooks,试图制作一个简单的倒计时计时器,它有几分钟和几秒钟会过期,如果需要的话,可以重新加载按钮来重置它,但是我一直在用正确的格式解析时间。以下是我所做工作的现场预览: import React,{useState,useffect}来自“React”; 导入“/styles.css”; 导出默认函数App(){ 常量[倒计时,设置倒计时]=使用状态({ 到期时间:10, 过期:假 }); useffect(()=>{ 让时间=倒计时。过期时间; 如果(时间>0){ const timerId=set

试图制作一个简单的倒计时计时器,它有几分钟和几秒钟会过期,如果需要的话,可以重新加载按钮来重置它,但是我一直在用正确的格式解析时间。以下是我所做工作的现场预览:

import React,{useState,useffect}来自“React”;
导入“/styles.css”;
导出默认函数App(){
常量[倒计时,设置倒计时]=使用状态({
到期时间:10,
过期:假
});
useffect(()=>{
让时间=倒计时。过期时间;
如果(时间>0){
const timerId=setTimeout(()=>{
设置倒计时({expirittime:time-1});
}, 1000);
return()=>clearTimeout(timerId);
}
});
返回(
剩余时间:{倒计时.到期时间}
setCountdown({ExpiritTime:10}}>Reset
);

}
如果以秒为单位存储所有状态,即120s==2分钟,则只需做一些简单的数学运算即可计算分和秒

minutes = Math.floor(time / 60)
seconds = time % 60
所以你的渲染可以是

<div className="timer">
  Time Remaining: {Math.floor(countdown.expirityTime / 60)}:
  {countdown.expirityTime % 60}
</div>
<button onClick={e => setCountdown({ expirityTime: 120 })}>Reset</button>

剩余时间:{Math.floor(countdown.expirittime/60)}:
{倒计时.到期时间%60}
setCountdown({ExpiritTime:120})}>Reset

如果以秒为单位存储所有状态,即120s==2分钟,则只需做一些简单的数学运算即可计算分和秒

minutes = Math.floor(time / 60)
seconds = time % 60
所以你的渲染可以是

<div className="timer">
  Time Remaining: {Math.floor(countdown.expirityTime / 60)}:
  {countdown.expirityTime % 60}
</div>
<button onClick={e => setCountdown({ expirityTime: 120 })}>Reset</button>

剩余时间:{Math.floor(countdown.expirittime/60)}:
{倒计时.到期时间%60}
setCountdown({ExpiritTime:120})}>Reset