簡體   English   中英

chrome擴展popup.js問題中的setTimeout函數

[英]setTimeout function in chrome extension popup.js issues

我有一個包含一系列功能的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和5應該是:

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

因為您將它們稱為內聯,所以沒有延遲。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM