gpt4 book ai didi

javascript - 无法传递作为 setTimeout 函数传递的匿名函数的参数

转载 作者:行者123 更新时间:2023-12-03 11:52:11 25 4
gpt4 key购买 nike

我在将值传递给匿名函数的参数时遇到问题,该函数作为 setTimeout 函数的参数传递。

我已经被困了 5 个多小时了...

请查看下面的代码并为我提供帮助。

提前致谢!!

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

}

最佳答案

您期望 nextUrl 作为 setTimeout 回调函数的参数。该变量是在父函数作用域中定义的,并且有您需要的值。

正确的代码是:

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

}

关于javascript - 无法传递作为 setTimeout 函数传递的匿名函数的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25777166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com