Nuxt/examples/web-worker/assets/js/Example.worker.js

20 lines
543 B
JavaScript
Raw Normal View History

// block for `time` ms, then return the number of loops we could run in that time:
2018-03-18 00:45:52 +00:00
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' })
2018-03-18 00:45:52 +00:00
}
})