javascript setTimeout不能识别函数参数
javascript setTimeout does not recognize function parameter
我正在写一个Google Chrome扩展。我使用setTimeout来降低对服务器的请求速度。但是setTimeout没有像预期的那样工作。它返回一个错误,说reqUrl没有定义。
基于对类似问题的答案,似乎这是一个超出范围的问题,我不明白如何解决它,除了使reqUrl成为一个全局变量,这似乎不是一个很好的解决方案。如果我去掉括号,它就会失去控制,完全没有时间延迟。
如何使这个工作?
下面是代码。我包含了slowdown函数,尽管我不认为它是问题的核心。
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;
}
};
setTimeout ({functionname},{超时},{param1}, {param2}…)
例子setTimeout(callMe, 1000, 'say','hello');
function callMe(p1, p2){
alert(p1+" "+p2); //alerts say hello
}
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());
你正在执行的JavaScript将看起来像这样:createDetailWindow(reqUrl)
,这实际上不是你想要的——你试图传递最初传递给openDetailPg
的字符串,对吗?所以你传递给setTimeout
的字符串需要适当地构造:"createDetailWindow('" + reqUrl + "')"
(假设reqUrl
总是被正确转义)。
顺便说一句,最好把东西浓缩成一个scscce,我花了一段时间才找到对setTimeout
的调用。
相关文章:
- 函数参数中的数据与指定变量之间的任何性能差异
- AngularJS:我可以跳过函数参数回调吗
- 为什么不'我们在javascript中使用函数参数的数据类型
- 使用闭包共享构造函数参数
- 函数参数的值错误
- 当一个重要的构造函数参数丢失时应该发生什么
- 阻止ReSharper将JavaScript函数参数放到新行中
- 根据函数参数将CSS值存储在var中,然后对其进行操作
- 使构造函数参数具有ES6类方法的特权
- 当客户端将两个按钮的javascript函数参数修改为相同时,SQL/PHP会更新多行
- Windows.Networking.EndpointPair构造函数参数
- 是否可以使用Sinon.JS检查函数参数绑定是否正确
- 大括号中的Javascript函数参数
- jQuery 无法使用 AJAX 调用访问函数内部的函数参数
- 将JSON字符串转换为函数参数
- eval()读取函数参数有多糟糕
- Javascript不能在一个命令中使用函数参数
- 函数参数在内部不可见,返回新的Promise(函数(resolve,reject).传递给被promise化的函数的
- ES6类构造函数参数
- 是否可以在javascript函数参数列表中调用多个函数