简体   繁体   English

将参数传递给setTimeout回调函数

[英]Pass parameter to setTimeout callback function

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. 所以我想将“x”传递给setTimeout回调函数。 But I am getting "x" as undefined inside the 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);

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 . 但请注意, 根据MDN,它不适用于IE < 10

When setTimeout invokes the callback, it doesn't pass any arguments (by default); setTimeout调用回调时,它不传递任何参数(默认情况下); that is, the argument x is undefined when the callback is invoked. 也就是说,在调用回调时,参数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() . 如果删除参数x ,函数体中的x将不会引用未定义的参数,而是引用在setTimeout()调用之外定义的变量。

var x = "hello";
setTimeout(function () { //note: no 'x' parameter
    console.log("setTimeout ... : " + x);
}, 1000);

Alternatively, if it must be 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);

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. 为我工作。

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);

It is because the function is called without passing it any argument: so x is 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);

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. setTimeout方法旨在将函数或代码片段作为其第一个参数,但在您的情况下,您已使用参数x的匿名函数,并且在不带任何参数的情况下调用它,因此它显示未定义,因为没有定义的具体函数需要x 。 What you can do is you can first define the function you want to call and then just call it in setTimeout method. 你可以做的是你可以先定义你想要调用的函数,然后在setTimeout方法中调用它。 See following code snippet: 请参阅以下代码段:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM