Refactor web worker implementation in main.js
This commit is contained in:
parent
53b82a8006
commit
c4db8b5e1e
27
src/main.js
27
src/main.js
@ -1,18 +1,15 @@
|
|||||||
if (typeof SharedWorker === "undefined") {
|
if (typeof SharedWorker !== "undefined") {
|
||||||
alert('当前浏览器不支持webworker')
|
const worker = new SharedWorker('/src/worker.js')
|
||||||
}
|
worker.port.onmessage = (e) => {
|
||||||
|
|
||||||
// 检查是否存在其它标签页, 如果存在不必建立 websocket 连接, 如果不存在, 则建立 websocket 连接
|
|
||||||
const worker = new SharedWorker('/src/worker.js')
|
|
||||||
worker.port.onmessage = (e) => {
|
|
||||||
console.log('worker.port.onmessage:', e.data)
|
console.log('worker.port.onmessage:', e.data)
|
||||||
}
|
}
|
||||||
worker.port.start()
|
worker.port.start()
|
||||||
|
const button = document.createElement('button')
|
||||||
const button = document.createElement('button')
|
button.innerText = 'click'
|
||||||
button.innerText = 'click'
|
button.onclick = () => {
|
||||||
button.onclick = () => {
|
|
||||||
worker.port.postMessage('hello, worker')
|
worker.port.postMessage('hello, worker')
|
||||||
|
}
|
||||||
|
document.body.appendChild(button)
|
||||||
|
} else {
|
||||||
|
console.log('当前浏览器不支持webworker')
|
||||||
}
|
}
|
||||||
|
|
||||||
document.body.appendChild(button)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user