Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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-传递函数时不是函数错误_Javascript - Fatal编程技术网

javascript-传递函数时不是函数错误

javascript-传递函数时不是函数错误,javascript,Javascript,我在以下代码段接收到未捕获类型错误:success不是一个函数: waitFor(document.getElementsByClassName('class'), validate); function waitFor(element, success) { if (element == null || element.length == 0) { setTimeout(function() { waitFor(element); }, 100); } els

我在以下代码段接收到
未捕获类型错误:success不是一个函数

waitFor(document.getElementsByClassName('class'), validate);

function waitFor(element, success) {
    if (element == null || element.length == 0) {
        setTimeout(function() { waitFor(element); }, 100);
    } else {
        success(element);
    }
}

function validate(element) {
    //do stuff
}
我的问题是为什么我会收到这个错误?下面正确定义了验证函数。我将其作为waitFor函数的参数传递,但在waitFor中未定义success。根据以下回答,我在javascript中将函数作为参数传递:

我错过了什么?

当你打电话时

setTimeout(function() { waitFor(element); }, 100);

success是未定义的,因此当您在
setTimeout
中调用
waitFor
时,可能会抛出一个错误

,您没有传递
success
setTimeout(waitFor,100,element,success),而是将其更正为
setTimeout(function(){waitFor(element,success);},100)我仍然收到相同的错误。确定它应该是
设置超时(waitFor,100,element,success)
。谢谢大家;)