This section guides you through installing Stopify and applying it to a simple
JavaScript program that runs forever and periodically prints the current time.
However, since the program never yields control to the browser’s event loop
(e.g., using setTimeout
), nothing will appear on the page and the browser
tab will eventually crash. Stopify will make this program behave more naturally
and actually show the output.
npm install -g stopify
input.js
.const elt = document.createElement("div");
document.body.appendChild(elt);
var i = 0;
var j = 0;
while (true) {
if (i++ == 10000000) {
j++;
elt.innerText = "Still running ... " + (new Date());
i = 0;
}
}
This program will make any web browser crash.
stopify input.js output.js
stopify.bundle.js
). Look up the path using the following command:stopify-path stopify.bundle.js
<html>
<body>
<script src="stopify.bundle.js"></script>
<script>
var runner = stopify.stopify("output.js");
runner.run(() => console.log("done"));
</script>
</body>
</html>
Finally, open the page in any browser. You’ll find that the program “just
works” and periodically prints the current time. In contrast, if you load
input.js
directly, it will not print anything and will eventually crash the
browser tab.