2011-05-19 121 views
2

在後臺腳本中,我向每個選項卡發送請求。我的問題是如何從回調函數中獲取響應的標籤?由於sendRequest是異步的,tab.id不能在callbock中使用。找出在Chrome擴展中發送響應的選項卡

for (var i = 0, tab; tab = tabs[i]; i++) { 
    chrome.tabs.sendRequest(tab.id, {play:0}, function(response) { 
     // do something here 
     // how do i get the tab.id from which the response come from? 
    }); 
} 

回答

3

您需要創建關閉:

for (var i = 0, tab; tab = tabs[i]; i++) { 
    chrome.tabs.sendRequest(tab.id, {play:0}, (function(tabId) { 
     return function(response) { 
      //tabId stores current tab id 
      console.log("response from:", tabId); 
     } 
    })(tab.id)); 
} 
相關問題