0
我正在做一個擴展名,我使用鉻存儲來存儲一個變量。實際上,我首先從用戶輸入一個定時器,然後在每T秒後刷新頁面,這些頁面由用戶輸入。所以要存儲這次我使用鉻存儲是這樣的:Chrome存儲提供未定義的存儲定時器
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
var currentTimer = request.timer;
if(currentTimer!=null){
currentTimer = currentTimer*1000;
alert(currentTimer);
chrome.storage.sync.set({'x': currentTimer}, function() {
console.log(currentTimer);
});
}
if(currentTimer==null){
chrome.storage.sync.get('x', function(items){
currentTimer = items.value;
});
//Here currentTimer is undefined.
}
});
任何人都可以幫助爲什麼currentTimer仍然未定義。我正在調試很長時間,但無法達到解決方案。
問題是隻要刷新頁面,currentTimer就會獲得NULL值,因爲它只能被用戶輸入一次。
@wOxxOm我通過他們走了,但沒有爲我工作:( – GitCoder
@wOxxOm能否請您詳細的解決方案爲,那麼你的你是什麼意思?不能立即返回值。您應該從sync.get回調中將新郵件發回給您的內容腳本。 ? – GitCoder
看看[那個問題](http://stackoverflow.com/q/14220321/3959875)。似乎最好的重複候選人,並提供了一些關於這個問題的很好的解釋。 – wOxxOm