javascript-在requestAnimationFrame之前调用的perfomance.now()-performance.now()具有较大的t
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-在requestAnimationFrame之前调用的perfomance.now()-performance.now()具有较大的t,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2476字,纯文字阅读大概需要4分钟。
内容图文
![javascript-在requestAnimationFrame之前调用的perfomance.now()-performance.now()具有较大的t](/upload/InfoBanner/zyjiaocheng/684/791b49812a154c76a028c535d7c80b9f.jpg)
所以我有一个简单的功能:
var start = function(){
lastFrame = performance.now();
requestAnimationFrame((t)=>{interval(t)});
}
还有我的间隔函数(仅出于测试目的,我已堵塞了每个rAF标记的值)
function interval(t){
console.log (t);
console.log(lastFrame);
}
现在,我已经阅读了following response to another question,但我只是听不懂这个人的答案的一部分.
The timestamp passed in to the requestAnimationFrame() callback is the time of the beginning of the animation frame. Multiple callbacks being invoked during the same frame all receive the same timestamp. Thus, it would be really weird if performance.now() returned a time before the parameter value, but not really weird for it to be after that.
“如果performance.now()返回参数值之前的时间,那真的很奇怪”?
为什么这会很奇怪?我以为Javascript是一种解释语言?这一点:
lastFrame = performance.now();
浏览器现在甚至都没有关于下一行的信息:
requestAnimationFrame((t)=>{interval(t)});
当然,如果您甚至在为requestAnimationFrame提供回调之前调用performance.now(),lastFrame的时间应该小于传递给requestAnimationFrame的t吗?
在此人的响应中,他列出了一个6步的过程,涉及请求动画帧.但是,他将Performance.now()调用列为最后一步.
浏览器在animationFrame请求之前已对其进行解释时,怎么可能是最后一步?
解决方法:
您要指出的答案是关于在rAF的回调中调用performance.now()的问题.您是在外面打电话,所以当然会被设置为之前的日期.
您的代码,简化了.
let t0 = performance.now(); // This is called now
requestAnimationFrame(t1=>func(t1)); // this will be called at next screen refresh
// (somewhere between 0 and 17 ms later)
另一个答案是尖的:
let f1 = time => {
window.calledTime = time;
while(wait(a_few_seconds)){}
}
let f2 = time => {
time === window.calledTime; // true !
performance.now() - time; // approximately a_few_seconds
}
requestAnimationFrame(f1);
requestAnimationFrame(f2);
因此,仅需说几句,就您的情况而言,正常的lastFrame设置在t之前,因为它是在rAF之前异步调用的.
另一个答案是关于链接的rAF调用,这些链接都堆叠在同一个函数调用中,因此,无论以前的回调执行时间如何,都将共享相同的timestamp参数.
现在,如果您需要的是原始时间戳记,则可以非常安全地使用performance.now()作为后备,因为它是作为参数发送的,但是在执行堆栈的第一个回调时才使用.
const loop = time => {
if(!time) time = performance.now(); // we called it out of a rAF routine
...
requestAnimationFrame(loop)
}
loop();
但是,大多数情况下,如果您使用的是rAF循环,那是因为您需要例程与屏幕同步.因此,等到下一个屏幕刷新后再开始循环才有意义,否则,您将在单个帧中两次执行此例程.
const loop = time =>{
...
}
requestAnimationFrame(loop); // call it directly through rAF
内容总结
以上是互联网集市为您收集整理的javascript-在requestAnimationFrame之前调用的perfomance.now()-performance.now()具有较大的t全部内容,希望文章能够帮你解决javascript-在requestAnimationFrame之前调用的perfomance.now()-performance.now()具有较大的t所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。