, , setTimeout:
, , setTimeout .
:
, , , JavaScript ( ); , , , , NodeJS .
: , ( setTimeout ), "", ( "" ). , , β ββ .
nextTick . , , , .
, setInterval :
let counter = 0;
let timer = setInterval(() => {
$("#ticker").text(++counter);
}, 100);
$("#hog").on("click", function() {
let x = 300000;
Promise.resolve().then(() => console.log(Date.now(), "Begin"));
next().then(() => console.log(Date.now(), "End"));
function next() {
if (--x > 0) {
if (x === 150000) {
Promise.resolve().then(function() {
console.log(Date.now(), "Middle");
});
}
return Promise.resolve().then(next);
} else {
return 0;
}
}
});
$("#stop").on("click", function() {
clearInterval(timer);
});
<div id="ticker"> </div>
<div><input id="stop" type="button" value="Stop"></div>
<div><input id="hog" type="button" value="Hog"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Hide resultHog, , , . - 300 000 , . , ( , , , ).
, , .
. setInterval , setInterval, , NodeJS, NodeJS setInterval .