Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将参数传递给setTimeout回调函数_Javascript_Jquery_Settimeout - Fatal编程技术网

Javascript 将参数传递给setTimeout回调函数

Javascript 将参数传递给setTimeout回调函数,javascript,jquery,settimeout,Javascript,Jquery,Settimeout,我有一些JS代码如下 var x = self.someAJAXResponseJSON; // x has some object value here setTimeout(function(x){ console.log("setTimeout ... : " + x); // But x is undefined here }, 1000); 所以我想把“x”传递给setTimeout回调函数。但是我在setTimeout中得到了未定义的“x” 我做错了什么 更新 有没有想过使

我有一些JS代码如下

var x = self.someAJAXResponseJSON; // x has some object value here
setTimeout(function(x){
    console.log("setTimeout ... : " + x); // But x is undefined here
}, 1000);
所以我想把“x”传递给setTimeout回调函数。但是我在setTimeout中得到了未定义的“x”

我做错了什么

更新

有没有想过使用DojoJS修复类似问题

setTimeout(dojo.hitch(this, function(){
    this.executeSomeFunction(x); // what shud be this
    console.log("setTimeout ... : " + x); // But x is undefined here
}), 1000);

在代码中,
console.log(x)
引用回调函数的
x
参数

只需从函数签名中省略它,您就可以:

setTimeout(function(){
  console.log("setTimeout ... : " + x); // now x is the global x
}, 1000);

setTimeout
调用回调时,它不会传递任何参数(默认情况下);也就是说,调用回调时,参数
x
未定义

如果删除参数
x
,函数体中的
x
将不会引用未定义的参数,而是引用在调用
setTimeout()
之外定义的变量

或者,如果它必须是一个参数,您可以将其作为参数传递给
setTimeout
(不过,请帮自己一个忙,并以不同的方式命名):


这是因为调用函数时没有传递任何参数:因此x是未定义的

如果您愿意使用不同的x参数调用它,则应将其包装在闭包中:


或者,您也可以在不创建闭包的情况下执行此操作

function myFunction(str1, str2) {
  alert(str1); //hello
  alert(str2); //world
}

window.setTimeout(myFunction, 10, 'hello', 'world');

但请注意,它在
IE<10

上不起作用,setTimeout方法设计为将函数或代码片段作为其第一个参数,但在您的情况下,您使用了一个带有参数x的匿名函数,该函数在调用时没有使用任何参数,因此它显示为未定义,因为没有定义具体的函数来使用x。您可以做的是,首先定义要调用的函数,然后在setTimeout方法中调用它。请参阅以下代码段:

var x = self.someAJAXResponseJSON;
function mylog(x){
  console.log("setTimeout ... : " + x);
}
setTimeOut(mylog(x),1000);

我自己遇到了这个问题,并查看了节点文档,要传递到函数中的参数作为setTimeout调用的第三个(或更多)参数,因此

myfunc = function(x){console.log(x)};
x = "test";
setTimeout(myfunc,100,x);

为我工作。

如果前面定义了
var x
,这不可能吗->
setTimeout(function(){console.log(“setTimeout…:”+x);},1000)。从
callback
中删除
x
并直接赋值?好吧,我认为这种情况下的目标应该类似于创建几个具有不同x值的函数。你能解释一下@testndtv吗?这能回答你的问题吗?这并不完全是“从不同的作用域调用函数”。函数打开一个新的作用域,该作用域中定义了一个标识符
x
,默认情况下该标识符未定义。您也不需要其他名称。它不会有任何效果。只是为了更好地理解它:我假设他对作用域的工作原理没有清楚的理解。我宁愿做
setTimeout((函数(x){…})。bind(null,x),1000)
then:)@pietro909你应该在答案中包含
bind
方法Thx..请你也看一下更新的问题好吗?
function myFunction(str1, str2) {
  alert(str1); //hello
  alert(str2); //world
}

window.setTimeout(myFunction, 10, 'hello', 'world');
var x = self.someAJAXResponseJSON;
function mylog(x){
  console.log("setTimeout ... : " + x);
}
setTimeOut(mylog(x),1000);
myfunc = function(x){console.log(x)};
x = "test";
setTimeout(myfunc,100,x);