JavaScript 版本的 ActionScript 的 Event.ENTER_FRAME 事件? [英] JavaScript's version of ActionScript's Event.ENTER_FRAME event?
问题描述
我正在尝试学习 JavaScript,我想知道 JavaScript 是否有一个事件侦听器,就像 ActionScript 的 ENTER_FRAME.基本上,我希望这个事件侦听器一直"侦听,而不仅仅是等待事件的任何特定实例(鼠标单击、键盘事件).
I am trying to learn JavaScript and I am wondering whether JavaScript has a event listener just like ActionScript's ENTER_FRAME. Basically, I want this event listener to listen "all the time" not just wait for any particular instance (mouse click, keyboard event) of event.
推荐答案
您正在寻找 setInterval(func, time)
.在让它像 ENTER_FRAME 一样工作的情况下,那么你会让时间变得非常少.因此,如果您想模仿每秒 30 次的帧速率:
You're looking for setInterval(func, time)
. In the case of making it work like ENTER_FRAME, then you would make time very small. So, if you wanted to imitate a frame rate of say, 30 times a second:
// you will need to make sure you have good scoping around the function param.
setInterval(function(){console.log('enterframe')}, 33)
// 33 is about 1000 milliseconds / 30.
实际上,setInterval
也在 Flash 中——flash.utils.setInterval
.
Actually, setInterval
is in Flash too -- flash.utils.setInterval
.
附带说明 -- 不幸的是,setInterval
(在 Flash 和 JS 中)可能会影响本机刷新率.在 Flash 中 ENTER_FRAME 避免了这种情况——您在 swf 重新渲染时进行渲染.在浏览器中,setInterval
根本无法做到这一点.
As a side note -- unfortunately, setInterval
(in both Flash and JS) can work against the native refresh rate. In Flash ENTER_FRAME avoids this -- you render when the swf re-renders. In the browser, well, setInterval
simply can't do that.
这篇关于JavaScript 版本的 ActionScript 的 Event.ENTER_FRAME 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!