Javascript 将参数传递给 setTimeout 回调函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32326721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass parameter to setTimeout callback function
提问by testndtv
I have some JS code as below;
我有一些 JS 代码如下;
var x = self.someAJAXResponseJSON; // x has some object value here
setTimeout(function(x){
console.log("setTimeout ... : " + x); // But x is undefined here
}, 1000);
So I want to pass "x" to the setTimeout callback function. But I am getting "x" as undefined inside the setTimeout.
所以我想将“x”传递给 setTimeout 回调函数。但是我在 setTimeout 中将“x”设为未定义。
What am I doing wrong ?
我究竟做错了什么 ?
UPDATED
更新
Any idea of fix for similar issue using DOJO JS
使用 DOJO JS 解决类似问题的任何想法
setTimeout(dojo.hitch(this, function(){
this.executeSomeFunction(x); // what shud be this
console.log("setTimeout ... : " + x); // But x is undefined here
}), 1000);
回答by Maciej Krawczyk
Alternatively you can do it without creating a closure.
或者,您可以在不创建闭包的情况下执行此操作。
function myFunction(str1, str2) {
alert(str1); //hello
alert(str2); //world
}
window.setTimeout(myFunction, 10, 'hello', 'world');
But note it doesn't work on IE < 10
according to MDN.
但请注意,IE < 10
根据 MDN,它不起作用。
回答by Purag
When setTimeout
invokes the callback, it doesn't pass any arguments (by default); that is, the argument x
is undefined when the callback is invoked.
当setTimeout
调用回调函数,它不传递任何参数(默认); 也就是说,x
当回调被调用时,参数是未定义的。
If you remove the parameter x
, x
in the function body won't refer to the undefined parameter but instead to the variable you defined outside the call to setTimeout()
.
如果删除 parameter x
,x
函数体中将不会引用未定义的参数,而是引用您在调用之外定义的变量setTimeout()
。
var x = "hello";
setTimeout(function () { //note: no 'x' parameter
console.log("setTimeout ... : " + x);
}, 1000);
Alternatively, if it mustbe a parameter, you can pass it as an argument to setTimeout
(do yourself a favor and name it differently, though):
或者,如果它必须是一个参数,您可以将其作为参数传递给setTimeout
(尽管帮自己一个忙并对其进行不同的命名):
var x = "hello";
setTimeout(function (y) {
console.log("setTimeout ... : " + y);
}, 1000, x);
回答by myfavoritenoisemaker
Ran into this myself and looked at the Node docs, arguments to be passed into the function come in as a 3rd(or more) parameter to the setTimeout call so...
我自己遇到了这个并查看了 Node 文档,要传递给函数的参数作为 setTimeout 调用的第 3 个(或更多)参数传入,因此...
myfunc = function(x){console.log(x)};
x = "test";
setTimeout(myfunc,100,x);
Worked for me.
为我工作。
回答by moonwave99
In your code, console.log(x)
refers to the x
parameter of the callback function.
在你的代码中,console.log(x)
指x
的是回调函数的参数。
Just omit it from function signature, and you'll be fine:
只需从函数签名中省略它,就可以了:
setTimeout(function(){
console.log("setTimeout ... : " + x); // now x is the global x
}, 1000);
回答by pietro909
It is because the function is called without passing it any argument: so xis undefined.
这是因为调用函数时没有传递任何参数:所以x是未定义的。
You should wrap it in a closure if you are willing to call it with different parameters for x:
如果您愿意为x使用不同的参数调用它,您应该将它包装在一个闭包中:
var x = self.someAJAXResponseJSON; // x has some object value here
setTimeout((function(y){
return(function() {
console.log("setTimeout ... : " + y);
})
})(x), 1000);
回答by Rahul Singh
setTimeout method is designed to take function or code snippet as its first argument but in you case you have taken an anonymous function with parameter x and which is called without taking any argument so it shows is undefined because there is no concrete function defined that takes x. What you can do is you can first define the function you want to call and then just call it in setTimeout method. See following code snippet:
setTimeout 方法旨在将函数或代码片段作为其第一个参数,但在您的情况下,您采用了一个带参数 x 的匿名函数,并且在不带任何参数的情况下调用该函数,因此它显示为未定义,因为没有定义采用 x 的具体函数. 您可以做的是首先定义要调用的函数,然后在 setTimeout 方法中调用它。请参阅以下代码片段:
var x = self.someAJAXResponseJSON;
function mylog(x){
console.log("setTimeout ... : " + x);
}
setTimeOut(mylog(x),1000);