我试图使计时器更新所有let
变量的值。但是很不幸,我的setInterval
功能不希望这样做。
我的问题在哪里?谢谢
function defineInterval(intervalInHours = 1, precision = 3600, disableInterval = 900, time = 200000) {
let interval = 0
let timeLeftInInterval = 0
let timePercent = 0
let timeDisabled = 0
setInterval(() => {
interval = intervalInHours * precision;
timeLeftInInterval = (time % interval);
console.log(interval, 'interval', timeLeftInInterval, 'timeLeftInInterval ', timeDisabled, 'timeDisabled', timePercent, 'timePercent') // 3600, 2000, 1600, 59 on output
if (disableInterval >= timeLeftInInterval) {
timeDisabled = disableInterval - timeLeftInInterval;
timePercent = (timeDisabled / disableInterval) * 100;
} else {
timeDisabled = interval - timeLeftInInterval;
timePercent = (timeDisabled / (interval++ - disableInterval) * 100);
interval++
}
}, 1000)
}
defineInterval();
答案 0 :(得分:0)
可能的问题:
interval = intervalInHours * precision;
都重置为3600。将其移到setInterval上方。if
语句中的值的操作,它没有任何影响。更改timeDisabled
和timePercent
不会进一步影响任何东西。每次超时后,它将给出相同的值。您是否错过了interval++
?interval++
语句中进行了两次else
。