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 回答