Created
February 4, 2020 14:44
-
-
Save leon0707/342abb9e4ca1732b8af7d294db0e7d24 to your computer and use it in GitHub Desktop.
main script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // some old browsers don't support web worker | |
| if (window.Worker) { | |
| // create a worker thread | |
| const worker = new Worker('web_worker.js'); | |
| // send the first msg to worker | |
| worker.postMessage('hello worker'); | |
| worker.addEventListener('message', function(event) { | |
| if (event.data === 'msg received, ack') { | |
| console.log('main thread received the ack'); | |
| // let worker terminate itself | |
| worker.postMessage('close your thread'); | |
| } else if (event.data === 'cannot terminated by itself') { | |
| worker.terminate(); | |
| } | |
| }); | |
| worker.addEventListener('error', function(err) { | |
| console.error(err.message, err.filename, err.lineno); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment