我很难确定如何在计时器应用程序中计算小时数。当我运行我的应用并设置时间(3小时:0分钟:5秒)时,我得到的倒计时是(3小时:80分钟:5秒)
我一直在搞乱hour变量,这似乎是问题的原因。因为计时器在添加小时参数之前工作正常。
//check if my hour const makes sense
const getRemaining = time => {
const hours = Math.floor(time / 60 / 60);
const minutes = Math.floor(time / 60);
const seconds = time - minutes * 60;
return {
hours: formatNumber(hours),
minutes: formatNumber(minutes),
seconds: formatNumber(seconds)
};
};
//check if this makes sense for the hours
start = () => {
this.setState(state => ({
remainingSeconds:
parseInt(state.selectedHours, 10) * 60 * 60 +
parseInt(state.selectedMinutes, 10) * 60 +
parseInt(state.selectedSeconds, 10),
isRunning: true
}));
this.interval = setInterval(() => {
this.setState(state => ({
remainingSeconds: state.remainingSeconds - 1
}));
}, 1000);
};
答案 0 :(得分:0)
您将剩余小时数计入剩余分钟数。因此,minutes
的值是180,会被截断为“ 80”(可能是formatNumber()
?)
要解决此问题,请在计算分钟之前从time
中减去剩余的整个小时数:
const getRemaining = time => {
const hours = Math.floor(time / 60 / 60);
time = time - hours * 60 * 60;
const minutes = Math.floor(time / 60);
const seconds = time - minutes * 60;
return {
hours: (hours),
minutes: (minutes),
seconds: (seconds)
};
};