2018-06-26 10:33:51 +00:00
|
|
|
// 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)))
|
2018-06-26 10:33:51 +00:00
|
|
|
} else {
|
|
|
|
self.postMessage({ hello: 'from worker' })
|
2018-03-18 00:45:52 +00:00
|
|
|
}
|
|
|
|
})
|