Toa*_*oad 7 javascript declaration typescript
我正在从谷歌为webgl-utils.js创建一个d.ts文件
我有一个问题,其中一个最后一行,全局对象中的方法是'猴子修补'(我认为这是正确的术语)
问题行如下:
/**
* Provides requestAnimationFrame in a cross browser way.
*/
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function FrameRequestCallback */ callback, /* DOMElement Element */ element) {
window.setTimeout(callback, 1000/60);
};
})();
Run Code Online (Sandbox Code Playgroud)
我如何在我的打字稿文件中声明这一点,以便在使用该函数时不会出现编译错误:
function tick()
{
requestAnimFrame(tick);
drawScene();
}
Run Code Online (Sandbox Code Playgroud)
我现在尝试过:
interface window
{
requestAnimFrame(): any;
}
Run Code Online (Sandbox Code Playgroud)
但这不会消除错误:
The name 'requestAnimFrame' does not exist in the current scope
Run Code Online (Sandbox Code Playgroud)
您正朝着正确的方向前进,但您需要定义所有变体:
interface Window {
requestAnimFrame(callback: any, element?: any): void;
webkitRequestAnimationFrame(callback: any, element?: any): void;
mozRequestAnimationFrame(callback: any, element?: any): void;
oRequestAnimationFrame(callback: any, element?: any): void;
}
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback, element?) {
window.setTimeout(callback, 1000/60);
};
})();
function tick() {
window.requestAnimFrame(tick);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1718 次 |
| 最近记录: |