工作人员使用的是Firefox和较早版本的Chrome,但不适用于最新的Chrome

时间:2015-05-26 07:44:58

标签: javascript angularjs google-chrome firefox web-worker

我有一个Canvas的指令,它与webWorker进行通信。在Firefox和Chrome的版本上< 43.0.2357.65(64位)一切正常,但在Chrome> = 43.0.2357.65(64位)上,该指令未接收到来自Worker的消息。

  restrict: 'E'
  replace: true
  templateUrl: 'some.html'
  link: ($scope, element, attrs) ->
    ...
    worker = new Worker('js/myworker.js')
    ...


    #
    # Initialize Worker with the canvas image
    #
    initializeWorker = (imgData, imgWidth, imgHeight, length) ->
      worker.postMessage(
        type: 'init'
        imgData: imgData
        width: imgWidth
        height: imgHeight
        length: length
        calculateBoundingBox: true
      )

    ...
    ...
    initializeWorker(...)

    worker.onmessage = (event) ->
      console.log('Worker message', event)

    worker.onerror = ->
      console.log('error on the web worker')

即使是像这样的虚拟工人:

var onmessage;

onmessage = function(event) {
  return postMessage('message');
};

我尝试使用Blob而不是加载`js'Worker文件,但问题仍然存在。

在Firefox和Chrome上< 43.0.2357.65(64位)我可以看到如下消息:

Worker message message { target: Worker, isTrusted: true, data: "message", origin: "", lastEventId: "", currentTarget: Worker, eventPhase: 2, bubbles: false, cancelable: true, defaultPrevented: false, timeStamp: 1432625927407334 }

但不是Chrome> = 43.0.2357.65(64位)。

1 个答案:

答案 0 :(得分:0)

试试这个!!

var onmessage;

self.onmessage = function(event) {
  return self.postMessage('message');
};

必须使用' self'