English 中文(简体)
如何将变数传递给匿名职能
原标题:How to pass variable to anonymous function
  • 时间:2012-04-18 10:31:11
  •  标签:
  • javascript

I want to pass variable setTimeoutfunction and do something with that. When I alert value of i it shows me numbers that i did not expected. What i m doing wrong? I want log values from 1 till 8.

var end=8;
for (var i = 1; i < end; i ++) {
       setTimeout(function (i) {
           console.log(i);   

       }, 800);
   }
问题回答

解决这一问题的标准方法是使用工厂功能:

var end=8;
for (var i = 1; i < end; i ++) {
       setTimeout(makeResponder(i), 800);
   }

function makeResponder(index) {
    return function () {
        console.log(index);   
   };
}

Live example > >: href=

在座右铭中,我们打字<>>>>>>>>>makeResponder,它交还了与通过的论点相近的职能(<>index,而不是i。 (Which是重要的)。 如果你刚刚从匿名职能中删除<代码>i的论点,则你的代码将部分运作,但从 > />>>>> />上看,并不是在最初排定时,所有职能都看见<代码>8/代码>。


<>Update> 摘自以下评论:

......如果这样说的话,将正确无误:settimeout(makeResponder(i),i*800;?

是的,如果你的目标是在大约800米之后发出每次呼吁,这将发挥作用:

Live example > > > >

I Trial setout(makeResponder(i),setInterval(i);

您不使用<条码>。


<>进一步更新<> 阁下:

我需要首先打印8个延迟,8个间隔,2个编印,7个延迟,......印有2个延迟......印有0个延迟。

你只是再次适用上述原则,第二次取消:

var end=8;
for (var i = 1; i < end; i ++) {
       setTimeout(makeResponder(i), i * 800);
   }

function makeResponder(index) {
    return function () {
        var thisStart = new Date();
        console.log("index = " + index + ", first function triggered");
        setTimeout(function() {
            console.log("index = " +
                        index +
                        ", second function triggered after a further " +
                        (new Date() - thisStart) +
                        "ms delay");
        }, index * 1000);
   };
}

Live example > > > > >> href=<>

我认为,现在,你们有一切必要的工具来推动这项工作。

您的问题是,在您的<代码>开启timeout(<>/code>)功能大火时,你指的是可变的<代码>i,到那时,i的数值发生了变化(该代码已到<>><>for>。 为保持每一规定时间的适当价值i, 您必须分别为每一条<代码>>准时(i查询。

以前使用工厂功能的回答是,只有罚款,但我发现,自我执行职能比工厂职能更容易进行分类和跟踪,但两者都能够发挥作用,因为两者都包含在关闭时所希望的变量,这样你就可以在固定停电时提及其固定价值。

在这方面,自我执行职能如何努力解决这一问题:

var end=8;
for (var i = 1; i < end; i ++) {
       (function (index) {
           setTimeout(function() {
               console.log(index);
           }, 800);
       })(i);
   }

为根据<代码>i的价值确定时间延误,请:

var end=8;
for (var i = 1; i < end; i ++) {
    (function (index) {
        setTimeout(function() {
            console.log(index);
        }, index * 800);
    })(i);
}

自我执行职能的价值通过i,该职能中包含该价值的论据称为/index。 因此,请参阅<代码>index,以使用适当的数值。


www.un.org/spanish/ecosoc 采用ES6

有了 Java(于2015年发布)ES6,你可以使用<条码><><>>>。 循环,这将为<<<>><>>>>><>>>>代码>的每个版本创造新的、单独的变量。 这是解决类似问题的“现代”办法:

const end = 8;
for (let i = 1; i < end; i++) {            // use "let" in this line
     setTimeout(function() {
         console.log(i);
     }, 800);
 }

之所以不工作的主要原因是,由于<编码> 暂停。 定在<代码>800和<编码>范围>后运行。

在实施时,<代码>i的价值已经发生变化。 因此,不能收到明确的结果。 正如TJ所说,围绕这一点开展工作的方法是双手职能。

function handler( var1) {
    return function() {
      console.log(var1);  
    }        
}

var end = 8;
for (var i = 1; i < end; i++) {     
   setTimeout(handler(i), 800);
}

rel=“nofollow”>





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.