为什么chrome.runtime.sendMessage导致我的回调在出现错误时被调用两次?

时间:2013-06-11 21:28:06

标签: google-chrome google-chrome-extension google-chrome-devtools google-chrome-app

我的后台脚本中有以下代码。我发送消息到另一个应用程序,但如果失败,则会调用两次(注意:我的回调被调用两次。我的代码调用sendMessage只调用一次)!

chrome.runtime.sendMessage(
    otherAppId,
    someObject,
    function (response)
    {
        var lastError = chrome.runtime.lastError;

        //This likely means it doesn't exist ("Could not establish connection. Receiving end does not exist.")
        if ( lastError && lastError.message.indexOf( "not exist" ) !== -1 )
        {
            ///This gets called twice!
            console.log( "we're here twice?!" );
        }

        //This is called once
        else console.log( "Was successful, so called only once" );
    }
);

有没有办法取消它,所以它再次停止尝试?为什么错误导致两次调用回调?

1 个答案:

答案 0 :(得分:0)

我正在定义自己的Function.prototype.bind(在我包含的JS库中),这导致了问题!不知道为什么这会导致如此多的错误并导致回调触发两次但是删除会导致所有错误消失!