Prevent warning () from stopping JavaScript execution

First of all, I know that JavaScript is single-threaded. Suppose I have a counter that displays its values ​​every second.

var counter = 0;
setInterval(function(){
        console.log("Interval:  " + ++counter);

    }, 1000);

At some point, I want to call code that stops the execution of a script. For example, a warning message.

setTimeout(function(){
    alert("Some alert message");
},10000);

At this point, when a warning message appears, the interval code will be stopped. Is there a trick to keep it from running?

0
source share
2 answers

, ( ), WebWorkers . , , , . , , worker.js, :

// file worker.js
var counter = 0;
setInterval(function(){
        console.log("Interval:  " + ++counter);

        // The worker will live until you kill it.
        if (counter > 100) self.close();  
}, 1000);

JavaScript , .

// Set up the new worker in a separate thread.
var worker = new Worker("worker.js");

setTimeout(function(){
    // This will not stop the worker from logging to console.
    alert("Some alert message");
},10000);

, alert() , .

Plunk .

+1

: , , ?

- ( ). , HTML popup. JQuery UI framework. , jQuery, HTML- .

JQuery :

HTML:

<div id="my-dialog">Interval: <span id="counter">0</span></div>

JavaScript:

// Configure the div as dialog, call this only once
$("#my-dialog").dialog({modal:true,
                        autoOpen: false,
                        buttons: {
                          Ok: function() {
                            $( this ).dialog( "close" );
                          }
                        }
});
var counter = 0;

var intervalID = setInterval(function() {
  // Update counter info
  $("#count").html(++counter);
  // Show dialog
  $("#my-dialog").dialog("open");

}, 1000);

+1

Source: https://habr.com/ru/post/1676242/


All Articles