How to freeze frozen thread?

14

Studying about JS:

  

There is only one thread to run your code, so you should avoid this code from blocking the thread as much as possible. (bfavaretto ♦)

Situation

It's not uncommon to end up locking the thread:

var error = function(){
    console.log("Error!!!");
}

window.onerror = error;

t.add(); // ReferenceError: t is not defined; Error!!!;

Doubt

After thread lock does it unlock it? using the error function?

    
asked by anonymous 27.11.2015 / 00:48

1 answer

5

There is no such thing as unlock , your script is an infinite loop case and can not stop without finishing the entire process, since javascript runs in a single event and what probably froze was the process and not just Thread, in some questions I commented on something similar:

The technique quoted with setTimeout for example only helps to minimize the problem (in case your script is more an error than a :) problem), but in these two answers I also mentioned:

What are possible solutions to what you want to achieve. jQuery for example uses AMD , see this is a part of jquery:

if ( typeof define === "function" && define.amd ) {
    define( "jquery", [], function() {
        return jQuery;
    });
}

However what will most likely help you is web workers which according to the documentation actually runs on a separate thread from the browser:

  

Web Workers provide a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface

     

Translating: Web Workers provide a simple means for Web content to run scripts to run Thread scripts in the background. worker thread can run tasks without interfering with the user interface.

Example with web workers

To create a process with web works it is necessary to create a separate .js file (let's call it meujavascript.js ) separated and calls it like this:

var meuWorker = new Worker("meujavascript.js");

To capture responses from the meujavascript.js script, you will have to use onmessage :

meuWorker.onmessage = function(e) {
   console.log('Mensagem recebida do Worker', e.data);
};

To send data to meuWorker.onmessage in content of meujavascript.js you should use:

postMessage("mensagem");

You can also send data to meujavascript.js like this:

var worker = new Worker("meujavascript.js");
worker.postMessage('Olá mundo!');

And in the content of meujavascript.js you should add something like:

onmessage = function(e) {
    console.log('Mensagem recebida do MAIN', e.data);
};
postMessage("mensagem");

Now comes the answer , you can "kill" a webworker, using terminate() , like this:

var worker = new Worker("meujavascript.js");
worker.postMessage('Olá mundo!');
worker.terminate();

Then you can work with a setTimeout on main (outside the worker) and check if the webworker does not send a message for a long time, there was probably a freeze, so if setTimeout time expires you execute the terminate .

    
27.11.2015 / 01:02