定时器修正

This commit is contained in:
Shikong 2023-07-23 15:15:48 +08:00
parent b8bcf1b3c0
commit a967f2dff2

View File

@ -22,7 +22,7 @@ const network = reactive({
bytesRecv: -1,
bytesSent: -1
},
lastTimestamp: 0,
lastTime: moment(0),
interval: <NodeJS.Timeout|undefined>undefined,
}
})
@ -31,6 +31,7 @@ const network = reactive({
function loopNetworkSumCounter(){
clearInterval(network.sumCounter.interval)
function loop(){
let fixed = 0
GetNetworkCounter(true).then(result=> {
let now = moment()
let recv = result[0].bytesRecv || 0
@ -39,23 +40,27 @@ function loopNetworkSumCounter(){
network.sumCounter.data.bytesRecv = network.sumCounter.data.bytesRecv == -1?recv:network.sumCounter.data.bytesRecv
network.sumCounter.data.bytesSent = network.sumCounter.data.bytesSent == -1?sent:network.sumCounter.data.bytesSent
if(now.diff(moment.unix(network.sumCounter.lastTimestamp),'s') <= 1){
// 避免因 webView 进入休眠/计时器误差 导致数据异常
if(now.diff(network.sumCounter.lastTime,'s') <= 1){
// 实时网速
network.sumCounter.rate.recv = recv - network.sumCounter.data.bytesRecv
network.sumCounter.rate.sent = sent - network.sumCounter.data.bytesSent
// 最高网速
network.sumCounter.rate.max.recv = Math.max(network.sumCounter.rate.max.recv, network.sumCounter.rate.recv)
network.sumCounter.rate.max.sent = Math.max(network.sumCounter.rate.max.sent, network.sumCounter.rate.sent)
}
network.sumCounter.data = result[0]
network.sumCounter.lastTimestamp = now.unix()
fixed = Math.max(1000 - now.diff(network.sumCounter.lastTime,'ms'), 0)
network.sumCounter.lastTime = now
})
// 每秒调用一次
network.sumCounter.interval = setTimeout(()=>{
loop()
},1000)
},1000 - fixed)
}
loop()
}