是否JavaScript的提供了一个高分辨率计时器? [英] Does JavaScript provide a high resolution timer?

查看:127
本文介绍了是否JavaScript的提供了一个高分辨率计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript的是否提供高分辨率定时器?

Does JavaScript provide a high resolution timer?

我已经写了一个从无到有的几个游戏引擎,在一些C,一些在Java中,有的在Flash中。我公司始终遵循相同的基本模式,当涉及到动画和交互式图形。具有以下设计创建一个基本的类/结构:

I've written a few game engines from scratch, some in C, some in Java, and some in Flash. I always follow the same basic model when it comes to animations and interactive graphics. Create a basic class/structure with the following design:

void init() { /* Called once, preload essential resources here. */ }
void update(double time) { /* Updates game/animation state using high resolution time. */ }
void render(double time) { /* Updates screen graphics using high resolution time. */ }

void run()
{
    double time;
    init();
    while (!done)
    {
        time = queryTime();
        update(time);
        render(time);
    }
}

时间是如此的重要,流畅的动画和游戏状态的计算。在本地code Windows中,我使用 QueryPerformanceCounter的() QueryPerformanceFrequency的()执行<$的作用C $ C> queryTime()在每个游戏循环,并通过更新时间/渲染。在Java中,我使用 System.nanoTime()

Time is so important to smooth animations and game state calculations. In native code Windows, I use QueryPerformanceCounter() and QueryPerformanceFrequency() to perform the role of queryTime() in each game loop and pass the time to update/render. In Java, I use System.nanoTime().

什么是JavaScript中的相同呢?也就是说,一些功能如 queryTime(),它以高精确度(亚毫秒)的返回时间值。据我所知,您可以在JavaScript中获得最佳的精度约为15 NBSP; MS ...这是可怕的动画

What's the equivalent in JavaScript? That is, some function like queryTime() which returns a time value with a high degree of accuracy (sub millisecond). From what I've heard the best accuracy you can get in JavaScript is about 15 ms ... which is horrible for animation.

推荐答案

毫秒就可以在JavaScript中最好的希望。而且,像你说的,是不是很准确。见堆栈溢出问题的 JavaScript中微秒时间

See @h3r3's answer below. It is the correct answer.

Milliseconds are the best you can hope for in JavaScript. And, like you said, it isn't very accurate. See Stack Overflow question Microsecond timing in JavaScript.

timer.js 声称提供高达微秒的分辨率,但它仅适用于< A HREF =htt​​p://en.wikipedia.org/wiki/Google_Chrome相对=nofollow>谷歌Chrome浏览器。

timer.js purports to provide up to microsecond resolution, but it is only available for Google Chrome.

更新:timer.js做的的支持微秒分辨率。它只是乘毫秒的1000计数。

Update: timer.js does not support microsecond resolution. It just multiplies millisecond count by 1000.

对不起,没有更好的消息!

Sorry, there isn't better news!

这篇关于是否JavaScript的提供了一个高分辨率计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆