Solution: setInterval cannot run normally in the background of the browser.



What is "Web Worker"

"Web Worker" provides a simple way for web content to run scripts in a background thread. Threads can perform tasks without disturbing the user interface. In addition, they can perform I/O using XMLHttpRequest. Once created, a worker can send a message to the JavaScript code that created it, by posting the message to the event handler specified by the code (and vice versa).


In the sample code below, we implement the simplest timer and let it trigger every 100 milliseconds.

            var timmer = new Worker(
        new Blob(
            { type: "application/javascript" }
timmer.onmessage = function (event) {
    console.log("get message");

Standard usage

In the above example, we used createObjectURL to create an address. Since the code we need to execute is relatively simple, this method will be more concise. When the code in the task we need to execute is more complicated, we can write the task separately as an independent js file. For details, please refer to the following code. There are more detailed introductions, you can refer to the reference materials at the top of the article.

            var timmer = new Worker('worker.js');