在 for 循環中將參數傳遞給 setTimeout (Passing argument to setTimeout in a for loop)


問題描述

在 for 循環中將參數傳遞給 setTimeout (Passing argument to setTimeout in a for loop)

I'm trying to learn how to pass an argument to setTimeout in a javacript for loop.  Here is the example code.  As it is currently written, setTimeout is passed the same exact i each time, not reflecting the different i's that are actually in the array.

var a=100;
for (i in array)
{   
    setTimeout("do_stuff(i, a)"), 2000);    
}

(I've seen somewhat similar questions and answers here, but the code examples are much more complicated.  Answering this most basic example could much help others with the same problem. Thanks!!!)

‑‑‑‑‑

參考解法

方法 1:

To use a string (which you shouldn't do), you 'd need to do this:

var a=100;
for (i in array)
{   
    setTimeout("do_stuff(" + i + ", a)"), 2000);    
}

A better answer would be to scope the i variable in a new function invocation, which returns an anonymous function to give to setTimeout().

function do_stuff( i, a ) {
    return function() {
        // do something with i and a
    }
}

var a=100;
for (i in array)
{   
    setTimeout(do_stuff( i , a ), 2000);    
}

Now do_stuff() returns a function that has a scoped reference to a new i and a variable. Because each call to do_stuff will have its own scope, the function you return will reference the correct values.


EDIT: Off topic, but if array is actually an Array, then you really shouldn't use for‑in because that's meant for enumeration. With an Array, you typically want iteration of numeric indices, and as such should use a standard for loop.

(by then user113716)

參考文件

  1. Passing argument to setTimeout in a for loop (CC BY‑SA 3.0/4.0)

#for-loop #arguments #javascript #settimeout #loops






相關問題

從R中的類引用列表中獲取類引用字段的最小值 (Get min value of a class reference field from a list of class references in R)

在 SQL Server 2008 中運行 WHILE 或 CURSOR 或兩者 (Running WHILE or CURSOR or both in SQL Server 2008)

danh sách trong python, vòng lặp for, mảng (list in python, loop for, array)

如何編寫一個程序來自動執行一組查詢 (How to write a procedure to execute set of queries automatically)

xPath 在使用 for-each 循環變量時找不到選擇器,但可以正常工作 (xPath not finding selector when using for-each loop variable, but works otherwise)

為什麼for循環重複輸出相同的記錄?JavaScript (Why for loop output same record repeatedly? JavaScript)

在 for 循環中將參數傳遞給 setTimeout (Passing argument to setTimeout in a for loop)

使用python匹配條件後如何從列表的開始迭代開始for循環 (How to start for-loop from the starting iteration of list after matching the condition using python)

BASH:在 for 循環中使用 continue (BASH: Using a continue in a for loop)

如何識別 For / Select / Loop 中的行號 (How do I identify the row number in a For / Select / Loop)

如何循環遍歷列表中的項目不斷附加在循環中的列表? (how to loop through a list where the items of the list are constantly appended in the loop?)

是否可以僅使用 for 循環來實現包含 for 循環的遞歸函數,該循環包含對上述函數的調用? (Can a recursive function containing a for loop that contains a call of the mentioned function be implemented using only for loops?)







留言討論