我正在尝试构建一个模拟时钟,秒针每秒旋转一次,最小指针每分钟旋转6度,小时指针每12分钟旋转6度。
这是代码和框:https://codesandbox.io/s/react-example-d7mes?file=/Clock.js
每当最小指针的角度为其中任何一个(12分钟度)时,我都会将时针旋转6度一次。[72, 144, 216, 288, 360]
这就是我正在做的事情:
let twelveMinDegrees = [72, 144, 216, 288, 360];
setInterval(() => {
this.setState(prev => ({
sec: prev.sec == 360 ? 6 : prev.sec + 6, //degrees
min: prev.sec == 354 ? (prev.min == 360 ? 6 : prev.min + 6) : prev.min, //degrees
hrs: (function(){ //degrees
const indx = twelveMinDegrees.findIndex(el => el == prev.min)
if(!minChanged && indx >=0){ //only change once for every 12min
minChanged = true;
let incHrs = prev.hrs + (6*indx);
console.log(incHrs);
return incHrs;
}else{
if(!twelveMinDegrees.includes(prev.min)){
minChanged = false;
}
return prev.hrs;
}
})()
}))
}, 1000)
但是时针不会改变,并且第二次被设置回other部分中的上一个值,并且返回的值被忽略,因为在状态更新之前,称为下一秒,它返回的仍然是旧值(不是在incHrselseprev.hrsif(!minChanged && indx >=0))
如何解决此问题?
FFIVE
沧海一幻觉
随时随地看视频慕课网APP
相关分类