【发布时间】:2021-12-08 16:27:01
【问题描述】:
我想编写一个自动执行最大值的系统。 8192 个不同的值,每秒持续 40 次。我想做的那种自动化是例如让一些值形成从 0 到 255 的正弦曲线,其他值从 50 上升到 80,其他值在 255 和 0 之间切换,等等。
我需要知道每次执行需要多长时间,如果执行时间超过允许的时间,它应该“丢帧”。
我的第一种方法是
// simplified code
const fps = 40
const executionStats : number[] = []
executionStats.length = fps * 10
const startFrame = new Date()
type SingleValue = {current: number, effectType: string, effectStarted: number}
const values : SingleValue[] = [...] // up to 8192 entries
// this is calculating the target value based on some lookups in other objects
// and based on when the effect started and in what tick we are in right now
const runSingleEffect = (value: SingleValue, tick: number) : number => {...}
const handler = async (tick: number) => {
values.forEach(value => {value.current = runSingleEffect(value, tick)})
}
setInterval(async () => {
const start = new Date()
await handler((start - startFrame) / fps);
const end = new Date()
executionStats.push(end - start)
executionStats.unshift()
}, 1000 / fps)
有没有比使用简单的setInterval 更好的方法? (显然我可以为执行统计使用环形缓冲区)但是有没有更好的方法来确保一次只运行一个处理程序?有没有这类东西的图书馆?
【问题讨论】:
标签: node.js typescript setinterval