chrome.runtime.sendMessage回调不会在异步函数内部触发

时间:2014-02-02 20:54:57

标签: javascript google-chrome google-chrome-extension

我在异步函数中执行sendMessage提供的回调时遇到问题。这是我的senario:

popup.js

chrome.runtime.sendMessage('hello from popup', function(res){
    alert(res);
});

Background.js

chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
    setTimeout(function () {
        sendResponse('hi from background');
    }, 300);
});

从后台脚本调用的sendResponse在超时(或任何异步方法)内部不会触发。回调的警报永远不会被执行。在超时之外,它会很好地触发(警报显示响应)。后台脚本控制台或弹出脚本控制台中没有错误。也许这是一个错误?

0 个答案:

没有答案