【发布时间】:2012-10-16 10:28:37
【问题描述】:
所以基本上我想要做的是有一个我可以添加到的函数队列。假设这些函数像在 requestAnimationFrame 中一样将内容绘制到画布上。
我尝试过的:
var drawQueue = [];
function animate() {
while (drawQueue.length !== 0) {
drawQueue.shift()();
}
requestAnimationFrame(function () {
animate();
});
}
我试图添加以下内容:
drawQueue.push(drawthing(0,0,0,0));
这只会运行函数并将 undefined 推送到数组,如果你这样做,它会如何工作:
drawQueue.push(function () {
console.log("derp");
});
或
drawQueue.push(functionName); //with no args
这里的问题是我需要将参数传递给我正在调用的大多数函数。
我的第二次尝试:
var drawQueue = [];
function animate() {
while (drawQueue.length !== 0) {
var data = drawQueue.shift();
Object.apply(data.func, data.args);
}
requestAnimationFrame(function () {
animate();
});
}
同时向队列中添加函数时:
drawQueue.push({func: functionName, args: [0,0,0,0]});
但我也无法让它工作,不是 100% 如何使用 Object.apply() 以及它到底在做什么......
所以我的问题是如何让我的函数队列或者有更好的方法来做到这一点?
【问题讨论】:
标签: javascript html node.js canvas webgl