数组移动只包含我想在画布上绘制的几个动作。基本上是deltaTime to sleep和draw命令。
这根本不起作用:(
for (var i=0; i< parsed.moves.length; i++)
{
var e = parsed.moves[i];
setTimeout(function(){drawCmd(context,e)}, e.deltaTime*1000);
//deltaTime are values btn 0-1, the sleep time i need to set btw commands
}
function drawCmd(canvasctx, e)
{
switch(e.type)
{
//case ...
}
}
它什么也没做,甚至没有调用drawCmd函数
答案 0 :(得分:1)
您的代码只会将所有drawCmd
延迟e.deltaTime*1000
,然后在i = 10
如果你打算“画画等待,画画等待”,试试这个:
(function loop(i){
var e = parsed.moves[i]; //e at current "i"
if(i< parsed.moves.length){ //if not equal to length
setTimeout(function(){
drawCmd(context,e); //draw in e.deltaTime*1000 milliseconds
loop(++i); //loop again
}, e.deltaTime*1000);
}
}(0)); //start with i=0
答案 1 :(得分:0)
您可能想要检查deltaTime的数据类型
setTimeout(function () { }, undefined * 1000); // <- Executes imidiately
setTimeout(function () { }, '1' * 1000); // <- Executes after 1 second
setTimeout(function () { }, 1 * 1000); // <- Executes after 1 second
setTimeout(function () { }, new Date() * 1000); // <- Executes after a long (i.e. 1337852997592 seconds) time