我正在使用HTML / Javascript编写Google Chrome扩展程序。我试图使用一个全局变量在两个函数之间传递信息,但是即使我在一个函数中分配我的变量,当我从另一个函数中读取它时它也没有改变。
var type = 0; //define global variable
window.onload=function(){onCreated()}; //set onCreated function to run after loading HTML
function onCreated()
{
chrome.history.search({'text': ''},function(historyItems){gotHistory(historyItems)});//search for historyItems and then pass them to the gotHistory function
}
function gotHistory(historyItems)
{
var idcount=0;//used to increment the ids of each new element added
for(var count=0; count < historyItems.length; count++)//go through each history item
{
chrome.history.getVisits({'url':historyItems[count].url}, function(visitItems){gotVisits(visitItems)}); //search for visitItems for the url and pass the results to gotVisists function (atm all this function does is assign the global variable to =3)
var body = document.getElementById("outputid");//find the body of the HTML
var newt = document.createElement("p");//create a new element
newt.setAttribute("id","url"+idcount);//give it a unique id
newt.innerHTML = historyItems[count].title;//set the text to say the title of the url
if(type != 0)//if the other function was successful, type=3 and the text should be green
{
newt.style.color="green";
}
body.appendChild(newt);//add the new element to the body
idcount++;
}
}
function gotVisits(visitItems)
{
//assign the global variable to be 3
type = 3;
}
但是,元素永远不是绿色。他们应该永远是绿色的。这意味着在函数gotVisits中,类型未成功分配给3。 谁能解释一下这里发生了什么?
干杯,
马特 p.s我意识到这里的gotVisits功能真的没用,但是我用它来证明一点。实际上,我将使用它将有用的信息传递给
答案 0 :(得分:0)
而不是:
var type = 0;
尝试:
window.type = 0;
您也可以选择使用闭包,例如:
(function() {
var type = 0;
var type = 0; //define global variable
window.onload=function(){onCreated()}; //set onCreated function to run after loading HTML
function onCreated()
{
chrome.history.search({'text': ''},function(historyItems){gotHistory(historyItems)});//search for historyItems and then pass them to the gotHistory function
}
function gotHistory(historyItems)
{
var idcount=0;//used to increment the ids of each new element added
for(var count=0; count < historyItems.length; count++)//go through each history item
{
chrome.history.getVisits({'url':historyItems[count].url}, function(visitItems){gotVisits(visitItems)}); //search for visitItems for the url and pass the results to gotVisists function (atm all this function does is assign the global variable to =3)
var body = document.getElementById("outputid");//find the body of the HTML
var newt = document.createElement("p");//create a new element
newt.setAttribute("id","url"+idcount);//give it a unique id
newt.innerHTML = historyItems[count].title;//set the text to say the title of the url
if(type != 0)//if the other function was successful, type=3 and the text should be green
{
newt.style.color="green";
}
body.appendChild(newt);//add the new element to the body
idcount++;
}
}
function gotVisits(visitItems)
{
//assign the global variable to be 3
type = 3;
}
})();
这样可以避免污染window
对象,无论如何都应该避免这种情况,并允许内部函数访问type
变量。
它应该做你想要的。
window.onload
的外部函数包装器也是多余的,只需执行:
window.onload = onCreated;
答案 1 :(得分:0)
看起来chrome.history.getVisits
异步执行回调,因此您首先尝试检查该变量,并在以后更新。您可以使用一对console.log
消息对此进行验证。
将其余代码移动到回调中,以便在正确的时间执行。