Why does not Chrome allow Web Workers to be run in JavaScript?

前端 未结 2 772
无人共我
无人共我 2020-12-15 14:30

If I try to use web workers through a JavaScript file, Chrome throws an error -

Uncaught SecurityError: Failed to create a worker: script at \'(path)

2条回答
  •  感情败类
    2020-12-15 15:19

    var cblock=`
    
    function workerFunc(e){
    
        console.info('Hello World!',e.data.msg)
        postMessage({msg:'Complete!'})
    }
    
    addEventListener('message',workerFunc)
    `    
    var a=new Worker(URL.createObjectURL(new Blob( [cblock], {type:'text/javascript'} )))    
    a.onmessage=function(e){ console.info('My worker called!',e.data.msg) }    
    a.onerror=function(e){ console.info( JSON.stringify(e,' ',2) ) }    
    a.postMessage({ msg:'Chrome WebWorkers work!' }) 
    
    // Hello World! Chrome WebWorkers work!
    // My worker called! Complete! 
    

提交回复
热议问题