Cách tốt nhất để nhận chênh lệch thời gian giữa lệnh gọi lại "window.requestAnimationFrame" trong javascript là gì?Javascript: Cách nhận chênh lệch thời gian giữa window.requestAnimationFrame
Tôi đã thử:
// create the best .requestAnimationFrame callback for each browser
window.FPS = (function() {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback) {window.setTimeout(callback, 1000/60);};
})();
// start animation loop
var dt, stamp = (new Date()).getTime();
function loop() {
window.FPS(loop);
var now = (new Date()).getTime();
var dt = now - stamp;
stamp = now;
}
// has "dt" the best accuracy?
tiềm năng trùng lặp: http://stackoverflow.com/questions/8279729/calculate-fps-in-canvas- sử dụng-requestanimationframe –
Khá chắc chắn rằng cuộc gọi lại được chuyển một tham số, dấu thời gian (trong yêu cầu fullAnAnationFrame gốc đầy đủ) nếu bạn đang tìm hỗ trợ cho polyfill, có những tham số mô phỏng tham số dấu thời gian tốt hơn thông số bạn có window.FPS polyfill. [Điều này] (https://gist.github.com/timhall/4078614) có lẽ là một trong những cái tốt hơn tôi đã nhìn thấy. Liên quan đến [this] (http://stackoverflow.com/questions/13241314/up-to-date-polyfill-for-requestanimationframe) SO câu hỏi – OJay
Btw, bạn hoàn toàn có thể bỏ qua IEFE và 'return' của nó, chỉ cần gán' window.raf =… || … || chức năng (cb) {…}; ' – Bergi