2

一連の関数を含む popup.js ファイルがあります。新しいタブが作成されたときに実行するように設定されている機能を実行しようとしていますが、少し遅れます。関数の例と私が試した解決策を次に示します。

// function.
function foo_bar()
{
 // some ajax call.
}

// try 1
setTimeout(foo_bar,1000);
EDIT:// executed without delay.

// try 2
setTimeout(function(){
//some ajax call.
},1000)
EDIT:// executed without delay.

// try 3
setTimeout(function(){
foo_bar();
},1000)
EDIT: // didn't seem to execute.

// try 4 and 5
window.addEventListener('load',foo_bar());
window.addEventListener('DOMContentLoaded',foo_bar());
// no delay takes place. The function completes before the page even loads.


// try 6
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {...}); 
// used the status from here to wait till page load is complete.
//problem with this is that sometimes the status doesn't get updated.

// try 7
// tried to delay the php script by using sleep(2), but ajax call would never complete.

誰でも助けてもらえますか?役立つかどうかはわかりませんが、スクリプトは背景またはコンテンツとして定義されていません。

編集:もう少し詳細。呼び出された関数は、リモート サーバーへの ajax 呼び出しを実行し、データに基づいて、chrome.tabs.executeScript を使用して、新しいタブが表示されたページを操作します。唯一の問題は、半分の時間でページの準備ができていないことです。

4

1 に答える 1

1

試行 4 と 5 は次のようになります。

window.addEventListener('load',foo_bar);
window.addEventListener('DOMContentLoaded',foo_bar);

インラインで呼び出したため、遅延は発生しませんでした。

于 2012-06-25T01:07:02.200 に答える