简体   繁体   English

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

[英]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.

相关问题 无法将父函数参数传递给子匿名函数 - Can't pass parent function argument to child anonymous function 将参数传递给匿名函数 - Pass an argument to anonymous function 作为函数参数传递的匿名函数的范围 - Scope of anonymous function passed as function argument js:无法将参数传递给setTimeOut中的匿名函数 - js: unable to pass parameter to anonymous function in setTimeOut 似乎无法将函数传递给node.js中的setTimeout参数数组(TypeScript) - Can't seem to pass function to setTimeout argument array in node.js (TypeScript) setTimeout传递了命名函数与匿名函数 - setTimeout passed named function vs. anonymous function 为什么不能将函数调用(而不是函数引用或匿名函数)传递给setTimeout()? - Why can I not pass a function call (rather than a function reference or an anonymous function) to setTimeout()? 如何将参数传递给使用setTimeout调用的函数? - How can I pass an argument to a function called using setTimeout? 将参数传递给函数作为参数传递给jQuery函数 - pass argument to function passed as argument to jQuery function 传递匿名函数时,如何在setTimeout中更改此/上下文? - how to change this/context in setTimeout when anonymous function is passed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM