Google Chrome拡張機能を作成しています。私はsetTimeoutを使用して、サーバーへの要求の速度を落とします。しかし、setTimeoutは期待どおりに動作していません。 reqUrlが定義されていないというエラーを返します。javascript setTimeoutが関数パラメータを認識しない
これは範囲外の問題であり、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;
}
};
**今後のアドバイス**次回は、あなたが問題を解決するためのコードの最低額を提供しています。あまりにも多くのコードを添付しています。コードの量が圧倒されているため(ロギングコールをコメントアウトしたものを含む)、あなたを助けようとする多くの人々は逃げるかもしれません。 –