为什么在使用 requestAnimationFrame 时使用这个表达式
why is this expression used when using requestAnimationFrame
为什么在使用requestAnimationFrame时使用这个if??
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
那段代码实现了requestAnimationFrame。它定义了一个全局函数requestAnimationFrame
(window
的所有成员都是全局变量)如果这样的函数不存在。
以下是通过评论对其功能进行的简短解释:
if (!window.requestAnimationFrame) // if requestAnimationFrame doesn't exist
window.requestAnimationFrame =
function(callback, element) { // define requestAnimationFrame
/* implementation detail */
};
这样的代码片段被称为 polyfills - 在旧 browsers/javascript 引擎中实现新版本 browser/javascript 引擎功能的代码。
为什么在使用requestAnimationFrame时使用这个if??
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
那段代码实现了requestAnimationFrame。它定义了一个全局函数requestAnimationFrame
(window
的所有成员都是全局变量)如果这样的函数不存在。
以下是通过评论对其功能进行的简短解释:
if (!window.requestAnimationFrame) // if requestAnimationFrame doesn't exist
window.requestAnimationFrame =
function(callback, element) { // define requestAnimationFrame
/* implementation detail */
};
这样的代码片段被称为 polyfills - 在旧 browsers/javascript 引擎中实现新版本 browser/javascript 引擎功能的代码。