Home › HTML Tutorial › Web workers API
Web workers API
Run heavy code off the main thread.
A worker runs a script in the background and talks to the page with postMessage, so the page stays responsive.
Example code
<p id='o'>Working...</p>
<script>
try{
const b=new Blob(['onmessage=e=>{let s=0;for(let i=0;i<=e.data;i++)s+=i;postMessage(s)}']);
const w=new Worker(URL.createObjectURL(b));
w.onmessage=e=>o.textContent='Sum: '+e.data;
w.postMessage(1e7);
}catch(e){o.textContent='Workers blocked: '+e.name}
</script>'