Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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 setTimeout无法识别函数参数_Javascript_Google Chrome_Settimeout - Fatal编程技术网

javascript setTimeout无法识别函数参数

javascript setTimeout无法识别函数参数,javascript,google-chrome,settimeout,Javascript,Google Chrome,Settimeout,我正在写一个谷歌浏览器的扩展。我使用setTimeout来降低对服务器的请求速度。但setTimeout未按预期工作。它返回一个错误,表示未定义reqUrl 根据对stackoverflow上类似问题的回答,这似乎是一个超出范围的问题,我不知道如何解决它,除了使reqUrl成为一个全局变量,这似乎不是一个很好的解决方案。如果我去掉括号,它就会失控,完全没有时间延迟 如何做到这一点 这是代码。我已经包含了slowdow函数,尽管我认为它不是问题的核心 openDetailPg(profileLin

我正在写一个谷歌浏览器的扩展。我使用setTimeout来降低对服务器的请求速度。但setTimeout未按预期工作。它返回一个错误,表示未定义reqUrl

根据对stackoverflow上类似问题的回答,这似乎是一个超出范围的问题,我不知道如何解决它,除了使reqUrl成为一个全局变量,这似乎不是一个很好的解决方案。如果我去掉括号,它就会失控,完全没有时间延迟

如何做到这一点

这是代码。我已经包含了slowdow函数,尽管我认为它不是问题的核心

openDetailPg(profileLink[currentLink]); 
function openDetailPg(reqUrl)
{
    console.log('openDetailPg at '+reqUrl);
    setTimeout("createDetailWindow(reqUrl)",slowDown());
    ++sendCount;
    timeOfLastRequest=new Date().getTime();
};
function createDetailWindow(detailUrl)
{
    console.log('createDetailWindow');
    chrome.tabs.create({windowId: mainWindowId, url: detailUrl}, 
    function (tab)
    {
        console.log('    OpenDetailPg Created Tab '+tab.id+' with slow down of '+slowDown().toFixed(0));
        chrome.tabs.executeScript(tab.id, {file: 'profile.js'});
    })
};
function slowDown()
{
    //console.log('  Slowdown: last interval '+ (new Date().getTime()-timeOfLastRequest)+' milisec.')
    if (new Date().getTime()-timeOfLastRequest>minDelay)
    {
        console.log('  Previous Delay Greater Than Minimum Delay, Resetting Speed Count');
        sendCount=1; 
        timeOfFirstRequest=new Date().getTime(); //else forget about it, reset time of first request
    }
    elapsedTime=new Date().getTime()-timeOfFirstRequest;
    avgSpeed = elapsedTime/sendCount;
    //console.log("  Started @ "+timeOfFirstRequest+" Current time "+new Date().getTime()+" Avg time fr 1st HTTPRequest "+avgSpeed.toFixed(0)+' milisec over '+sendCount+' Req');
    if (avgSpeed<minDelay)
    {
        //console.log("  Delaying request by "+((minDelay-avgSpeed).toFixed(0))+" milisecs");
        return minDelay-avgSpeed;
    }
    else
    {
        //console.log('  No Delay on Request');
        return 1;
    }
};
openDetailPg(profileLink[currentLink]);
函数openDetailPg(请求URL)
{
log('openDetailPg at'+reqUrl);
setTimeout(“createDetailWindow(reqUrl)”,slood();
++发送计数;
TimeOfLastreRequest=新日期().getTime();
};
函数createDetailWindow(detailUrl)
{
log('createDetailWindow');
create({windowId:mainWindowId,url:detailUrl},
功能(选项卡)
{
log('OpenDetailPg Created Tab'+Tab.id+',减速为'+slood().toFixed(0));
executeScript(tab.id,{file:'profile.js'});
})
};
函数(
{
//log('slood:last interval'+(new Date().getTime()-timeOfLastRequest)+'milisec'))
if(new Date().getTime()-timeOfLastRequest>minDelay)
{
日志(‘上一次延迟大于最小延迟,重置速度计数’);
sendCount=1;
timeOfFirstRequest=new Date().getTime();//否则,请忽略它,重置第一个请求的时间
}
elapsedTime=new Date().getTime()-timeofirstrequest;
avgSpeed=elapsedTime/sendCount;
//console.log(“Started@”+timeofirstrequest+“Current time”+new Date().getTime()+“第一次HTTPRequest的平均时间”+avgSpeed.toFixed(0)+“毫秒超过”+sendCount+“请求”);

如果(avgSpeed您正在执行的JavaScript看起来是这样的:
createDetailWindow(reqUrl)
,这实际上不是您想要的——您正在尝试将最初传入的字符串传递到
openDetailPg
,对吗?因此,您要传递到
setTimeout
的字符串需要适当构造:
“createDetailWindow(““+reqUrl+”)”
(假设总是正确转义
reqUrl

顺便说一句,最好将内容压缩为一个,我花了一段时间才找到对
setTimeout

的调用,尝试如下:

function openDetailPg(reqUrl)
{
    console.log('openDetailPg at '+reqUrl);
    setTimeout(function(){createDetailWindow(reqUrl)},slowDown());
    ++sendCount;
    timeOfLastRequest=new Date().getTime();
};
setTimeout(function() { createDetailWindow(reqUrl); }, slowDown()); 
试试这个:

setTimeout(function(){ createDetailWindow(reqUrl) },slowDown());

您需要为此使用匿名函数,例如:

setTimeout(function(){createDetailWindow(reqUrl)},slowDown());
setTimeout({functionname},{timeout},{param1},{param2}…)

范例

setTimeout(callMe, 1000, 'say','hello');
function callMe(p1, p2){
alert(p1+" "+p2); //alerts say hello
}

(其他使用闭包的答案比我的答案要干净得多。)将字符串传递给
setTimeout
是一项相当过时的功能。因此我的后续评论。然而,我的评论实际上解释了OP看到错误的原因。当你们忙着思考我的代码时,也许有人可以向我解释为什么删除括号时它会失控?因为在第二个示例中您正在执行createDetailWindow并将其结果返回给setTimeout。setTimeout需要一个函数参数(或字符串,但正如所指出的,ew)。这就是为什么所有示例都会传入一个匿名函数,包括reqUrl参数。下次的建议会提供最少的代码来帮助您解决问题。您附加了太多的代码。许多试图帮助您的人可能会拒绝,因为代码量太大了(包括那些被注释掉的日志调用)。这很好。谢谢。但是为什么需要在这里使用匿名函数?我知道它解决了作用域问题,但我不明白为什么。有几种方法可以做到这一点。
setTimeout
函数在开始时param需要函数或字符串的处理程序。如果字符串传递,它的工作原理与
eval
functio完全相同n但使用闭包时,传递到该字符串中的变量是未定义的。当传递简单处理程序时,
setTimeout
调用该处理程序。当传递匿名函数时,
setTimeout
认为传递了处理程序。使用闭包时,如果之前声明了,则可以在匿名函数中看到变量。但通常可以将任何内容传递给匿名函数:
function(){var test=test2+test3;for(var i=0;i