嵌套函数在Mozilla浏览器中不起作用

时间:2014-08-18 13:20:47

标签: javascript cross-browser mozilla

我在我的应用程序中调用了以下函数

function workerCall() {
    debugger;
    if (typeof (Worker) !== "undefined") {
        var worker = new Worker("Scripts/worker.js");
        worker.onmessage = workerResultReceiver;
        worker.onerror = workerErrorReceiver;
        worker.postMessage({ 'username': Username });
        function workerResultReceiver(e) {
            $('.NotificationCount').html(e.data);
            if (parseInt(e.data) != 0 && currentPage == "Alert") {
                StateFlag = false;
                $('.Notification').show();
                $('.Drildown').each(function () {
                    var temp = this.id;
                    if ($('#' + temp).attr('expand') == "true") {
                        currentTab = temp;
                        StateFlag = true;
                    }
                });
                currentScrollPosition = $('body').scrollTop();
                GetAlerts();
            } else {
                $('.Notification').hide();
            }
        }
        function workerErrorReceiver(e) {
            console.log("there was a problem with the WebWorker within " + e);
        }
    }
    else {

    }
}

该方法将在IE,Chrome中执行但是当来到Mozilla时出现错误 ReferenceError:未定义workerResultReceiver 。我该如何解决此错误?

1 个答案:

答案 0 :(得分:1)

这是因为您正在引用尚未创建的函数。你需要把它放在:

worker.onmessage = workerResultReceiver;
worker.onerror = workerErrorReceiver;

上方

function workerErrorReceiver

行或范围的末尾。