Use Web Workers for running scripts in background threads.
Source Code
if (window.Worker) {
const myWorker = new Worker('worker.js');
myWorker.postMessage('Hello');
myWorker.onmessage = function(e) {
console.log('Message received from worker', e.data);
};
}