2011-08-09 56 views
1

我试图做一个setTimeout,我将变量传递给setTimeout()内被调用的函数。经过一些初步的失败,然后使用谷歌我发现一个网站,描述如何使用闭包做到这一点。我几乎跟着例子,但我不断收到一条错误消息:经过参数列表javascript关闭错误

此错误消息正在呼吁setTimeout的,但据我可以告诉一切被关闭

缺失)。任何帮助,将不胜感激:

var textureAtlas = new Image() 

function init() { 

    textureAtlas.src = "images/textureatlast1.png"; 
    var textureAtlasCoords = new Array("0,0", "100,0", "200,0", "300,0", "400,0", "500,0", "600,0"); 

    var canvas = document.getElementById('textureAtlas'); 

    if (canvas.getContext){ 

     var ctx = canvas.getContext('2d'); 

     for(var c=0; c<textureAtlasCoords.length; c++) { 

      var thisCoord = textureAtlasCoords[c]; 
      var thisCoordSplit = thisCoord.split(","); 
      var thisX = thisCoordSplit[0]; 
      var thisY = thisCoordSplit[1]; 

      var a = setTimeout(animate(){myFunction(ctx, textureAtlas, thisX, thisY); ctx=null, textureAtlas=null, thisX=null, thisY=null},1000); 
     } 

    } else { 
     alert("Looks like your browser doesn't support HTML5"); 
    } 

} 

function animate() { 

    ctx.drawImage(thisImg,thisX,thisY, 1024, 451, 0, 0, 1024, 451); 

} 
+0

在setTimeout调用,是“有生命的”一个错字?或者你是否想要调用'animate'? – Lobstrosity

+0

动画后的“{...}”是什么? –

+0

如果它帮助我与这里给出的示例工作:http://www.makemineatriple.com/2007/10/passing-parameters-to-a-function-called-with-settimeout – PruitIgoe

回答

1

另外:

function animate() { 

    ctx.drawImage(thisImg,thisX,thisY, 1024, 451, 0, 0, 1024, 451); 

} 

上下文这里将引发 '未定义',作为

var ctx = canvas.getContext('2d'); 

将只在init的范围内可用。

你不严格需要一个“;”第一行之后:

var textureAtlas = new Image() 

然而,这将是一个非常好的主意,请参见:does javascript require ';' at the end of a line of code?

+0

感谢您注意到失踪;这是我的一个错字。 – PruitIgoe

+0

至于范围评论,我最初测试这个代码,它工作正常:imgDraw(ctx,thisImg); ...功能imgDraw(CTX,thisImg){ctx.drawImage(thisImg,0,0,1024,451,0,0,1024,451);} ...你说动画()将抛出一个不确定的,因为它被包含在setTimeout()中? – PruitIgoe

+0

我确实在想,实际上,封闭是一场噩梦,很少有解释/记录,我认为它可能是可用的。关闭的麻烦在于他们可能会因为你不期望的原因而工作。 :) – nicodemus13

2

我不完全清楚你想要安排在这里。

我可以告诉你的setTimeout它可以是一个函数文本或函数引用,就像这样:

  • 功能:

    setTimeout(nameOfMyFunction, 1000); // reference -- note, NO parentheses here 
    
    setTimeout(function() { // literal -- the function being executed is "anonymous" 
         /* body of function here */ 
        }, 
        1000); 
    

    在第一个语法,需要注意两件事情很重要必须在其他地方正常定义nameOfMyFunction;

  • 使用此语法,您不能将任何参数传递给nameOfMyFunction

如果通过一些ARGS是很重要的,那么你就可以在通过他们,像这样的匿名函数包裹调用:

setTimeout(function() { 
     nameOfMyFunction(someArg, otherArg); 
    }, 
    1000); 

而且目前还不清楚是什么myFunction是。 myFunction是否准备animate使用的绘图环境?或者是其他的一次性操作,应该在animate之前发生?

+0

(及其他)我想运行在断textureatlas的动画,animate函数将收到的纹理图像的x/y坐标从我挂到上面的意见是,使用封闭我将能够通过该网站我的理解(这可能是完全错误的) - Atlas和画布,以将其放置在 – PruitIgoe

+0

参考,添加一些更详细变量的函数称为setTimeout - 在这种情况下为动画,因为您无法通过函数传递变量并将它们作为第三个参数添加到setTimeout引发IE中的问题。 – PruitIgoe

0

替换:

var a = setTimeout(animate(){myFunction(ctx, textureAtlas, thisX, thisY); ctx=null, textureAtlas=null, thisX=null, thisY=null},1000); 
    } 

通过

var a = setTimeout(animate, 1000); 

我不知道你为什么包括animate(){...}

+0

我试图传递变量的动画() – PruitIgoe

1

试穿setTimeout加字function

 var a = setTimeout(function animate() { 
      myFunction(ctx, textureAtlas, thisX, thisY); 
      ctx = null, textureAtlas = null, thisX = null, thisY = null 
     }, 1000); 
+0

我在写的setTimeout错误的:var A = setTimeout的(函数(){动画(textureAtlas,thisX,thisY);},400); – PruitIgoe