Javascript code that prints out (using console.log) a sequence of numbers from 1 to 1,000,000 without blocking the interactions with the browser. For example, while that piece of Javascript is running, mouse, keyboard events etc should work fine and browser doesn't hang.
You can create a Web worker and let it handle your request in the backend
In your html file
index.html
<script>
// Check if your browser supports worker
if (window.Worker) {
var worker = new Worker('worker.js');
worker.postMessage("Start Logging");
}
</script>
worker.js
onmessage = function(e) {
const result = e.data;
if(result == "Start Logging"){
for(let i = 1; i<= 1000000;i++){
console.log(i)
}
postMessage('Done Loggind')
}
}
Web Workers can run your script in background without intercepting your main thread