Sorry for English but I don't know Polish. I use google translate. I want to point one thing to you. Try to experiment with this condition which Desu wrote:
return isBreakfestNow.counter++ >= 10;
You need to replace 10 with some really big number or even try to return false from isBreakfestNow. See what will happen when you run such code in a browser.
Because you have one thread for tab when you run long time processes you need to free this thread from time to time. If you don't do that browser will
be in non responding state or even system will offer user to kill the script or browser. Try to run this in FireFox console or in other browser:
function isBreakfestNow() {
return isBreakfestNow.counter++ >= 100000000000;
}
isBreakfestNow.counter = 0;
while (!isBreakfestNow()) {
console.log("Śniadania nie ma.");
}
console.log("Zapraszamy na śniadanie.");
There is many approaches to fix this behaviour. The most popular is to use setTimeout method.
This is example of the simplest approach to free tab thread from time to time. The
general idea is to split long process into set of little processes.
/**
* @author Georgi Naumov
* [email protected]
*/
function isBreakfestNow() {
return isBreakfestNow.counter++ >= 100000000000;
}
isBreakfestNow.counter = 0;
setTimeout(function loop() {
if (!isBreakfestNow()) {
console.log("Śniadania nie ma.");
setTimeout(loop, 0);
} else {
console.log("Zapraszamy na śniadanie.");
}
}, 0);
You can use promises to made code not so stinking.
/**
* @author Georgi Naumov
* [email protected]
*/
function isBreakfestNow() {
return isBreakfestNow.counter++ >= 10;
}
isBreakfestNow.counter = 0;
(function () {
return new Promise(function (resolve) {
setTimeout(function loop() {
if (!isBreakfestNow()) {
console.log("Śniadania nie ma.");
setTimeout(loop, 0);
} else {
resolve();
}
}, 0);
});
}()).then(function () {
console.log("Zapraszamy na śniadanie.");
});