1

我的后台脚本中有以下代码。我向另一个应用程序发送一条消息,但如果它失败了,它会被调用两次(注意:我的回调被调用了两次。我的代码调用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" );
    }
);

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

4

1 回答 1

0

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

于 2013-06-11T23:40:01.677 回答