【发布时间】:2021-09-13 06:30:20
【问题描述】:
我的代码如下所示:
async run(minutesToRun: number): Promise<void> {
await authenticate();
await this.stock.fillArray();
await subscribeToInstrument(this, this.orderBookId);
await subscribeToOrderbook(this, this.orderBookId);
await this.updateLoop(minutesToRun);
}
sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async updateLoop(minutesToRun: number) {
const startTime = new Date();
const timeDiff = compareTimestamps(startTime, this.currentTime);
while (timeDiff < minutesToRun) {
console.log(timeDiff);
this.currentTime = new Date();
this.timeStampArray.push(this.currentTime);
console.log(this.timeStampArray);
await this.stock.updateData();
await this.sleep(60000);
}
}
但是对我来说,在任何其他函数运行之前更新数据并不重要,更重要的是 updateData 调用每分钟发生一次,因为我需要根据一致的时间间隔分析数据。现在它在 1m 100ms 到 1m 300ms 之间波动。有没有办法确保这种情况,即使其他功能在两者之间运行?
【问题讨论】:
-
你试过
setInterval吗? -
您是否已经看过这个问题,如果是,有什么不同? stackoverflow.com/questions/196027/…
标签: javascript typescript loops settimeout