[英]can't pass an argument of anonymous function that is passed as setTimeout function
I'm having a trouble to pass a value to argument of anonymous function which is passed as an argument of setTimeout function. 我在将值传递给匿名函数的参数(作为setTimeout函数的参数传递)时遇到麻烦。
I've been stuck for more than 5 hours already... 我已经被困了5个多小时了...
Please take a look at the code below and give me a help. 请查看下面的代码并给我帮助。
Thanks in advance!! 提前致谢!!
if (_que.length > 0){
var nextUrl = _shiftNextUrlFromQue();
//here, the value for nextUrl exists.the value is url string.
console.log(nextUrl);
setTimeout(function(nextUrl) {
//here, the value is undefined.
_analyzeUrl(nextUrl);
}, 10000
);
}
You are expecting nextUrl as an argument to the setTimeout callback function. 您期望将nextUrl作为setTimeout回调函数的参数。 That variable is defined in the parent function scope and there is the value you need. 该变量在父函数作用域中定义,并且有您需要的值。
The correct code would've been: 正确的代码应该是:
if (_que.length > 0) {
var nextUrl = _shiftNextUrlFromQue();
//here, the value for nextUrl exists.the value is url string.
console.log(nextUrl);
setTimeout(function() { //HERE!... the timeout callback function has no argument
_analyzeUrl(nextUrl);
}, 10000
);
}
Don't pass it, you're nullifying it http://jsfiddle.net/5cckcrhj/ 不要通过它,您要取消它http://jsfiddle.net/5cckcrhj/
setTimeout(function() {
_analyzeUrl(nextUrl);
}, 10000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.