定时器([name])

此类是 [page:Clock] 的替代品,具有不同的 API 设计和行为。目标是避免随着时间的推移 [page:Clock] 中变得明显的概念缺陷。

导入

[name] 是一个附加组件,必须显式导入。请参阅 [link:#manual/introduction/Installation Installation / Addons].

import { Timer } from 'three/addons/misc/Timer.js';

代码示例

const timer = new Timer(); function animate( timestamp ) { requestAnimationFrame( animate ); // timestamp is optional timer.update( timestamp ); const delta = timer.getDelta(); // do something with delta renderer.render( scene, camera ); }

例子

[example:webgl_morphtargets_sphere WebGL / morphtargets / sphere]

构造函数

Timer()

方法

[method:Number getDelta]()

返回以秒为单位的时间增量。

[method:Number getElapsed]()

返回经过的时间(以秒为单位)。

[method:this setTimescale]( [param:Number timescale] )

设置一个时间刻度,缩放 [page:.update]() 中的时间增量。

[method:this reset]()

重置当前模拟步骤的时间计算。

[method:this dispose]()

可用于释放所有内部资源。通常在不再需要计时器实例时调用。

[method:this update]( [param:Number timestamp] )

timestamp -- (可选)当前时间(以毫秒为单位)。可以从 [link:https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame requestAnimationFrame] 回调参数中获取 。如果未提供,当前时间将由 [link:https://developer.mozilla.org/en-US/docs/Web/API/Performance/now performance.now] 确定。

更新定时器的内部状态。该方法应该在每个模拟步骤以及对计时器执行查询之前调用一次(例如通过 [page:.getDelta]())。

源代码

[link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/misc/Timer.js examples/jsm/misc/Timer.js]