gpt4 book ai didi

dart - 在Dart网络应用程序中,比新的DateTime更精确的是什么?

转载 作者:行者123 更新时间:2023-12-04 13:51:48 25 4
gpt4 key购买 nike

对于我来说,获取新的DateTime不够准确。浏览器中的日期对象以毫秒为单位给出整数。我希望能够获得比标准日期对象更精确的时间。这可能吗?我怎样才能做到这一点?

最佳答案

使用window.performance.now()获得单调的高分辨率时间。 now()函数以小数形式返回一个以微秒为单位的 double 值。

这是一个例子:

import 'dart:html';

main() {
var time = window.performance.now();
print(time); // 12123.24341221
}

请注意, now()不是典型的“从纪元开始的时间”。相反,它是 window.performance.timing.navigationStart的增量。
navigationStart字段定义为(来自 spec):

This attribute must return the time immediately after the user agent finishes prompting to unload the previous document. If there is no previous document, this attribute must return the same value as fetchStart.


window.performance.now()时间戳很棒,因为它不受时钟偏移的影响,并且比获取新的 DateTime更准确。

如果您的应用程序是由 requestAnimationFrame驱动的(如果不是,为什么不呢?:),那么您已经有了一个高分辨率的时间戳!
animationFrame返回的Future带有高分辨率时间戳:

Future<num> animationFrame;

您可以像这样使用它:

gameLoop(num highResolutionTime) {
// stuff
window.animationFrame.then(gameLoop);
}

main() {
window.animationFrame.then(gameLoop);
}

关于dart - 在Dart网络应用程序中,比新的DateTime更精确的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15738046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com