Nuxt/examples/web-worker/assets/js/Example.worker.js
Jon Ege Ronnenberg 1b46a95b06 Webworker (#3480)
* added missing eslint-loader that prevented the example to run and changed freeWorker to terminate a worker and create a new since we can not really free a worker

* proper remove long running worker from longRunningWorkers array when done

* polish web-worker example

set nuxt to 'latest' version

this will change

this will also change
2018-06-26 12:33:51 +02:00

20 lines
543 B
JavaScript

// block for `time` ms, then return the number of loops we could run in that time:
function expensive(time) {
let start = Date.now(),
count = 0
while (Date.now() - start < time) count++
return count
}
// Respond to message from parent thread
self.addEventListener('message', (event) => {
console.log('worker', event.data)
if (event.data.action === 'expensive' && event.data.time) {
// Post data to parent thread
self.postMessage(expensive(Number(event.data.time)))
} else {
self.postMessage({ hello: 'from worker' })
}
})