Hello - a feature suggestion
There is no "start of the code" other than the time when the JavaScript is parsed. But as JavaScript can be in multiple places (e.g. included in script tags somewhere in the page) and even be added at run time, it is not clear, what would need to be evaluated again and what not. Also the page may be in a different state from what it was originally in the beginning, so without reloading the page, your code would meet preconditions it did not expect.
From your StackOverflow-question:
"Given the |keydown| event is triggered only once, I consider sending the interpreter back to the start after the |keydown| event was triggered disposably, and without refreshing the page (Sorry if it seems absurd, I'm new to JS and failed finding the source of the bug)."
You may use the debugger and set a breakpoint to find your bug.
i have this problem often and emulate “stop" by wrapping the entire-code inside a function (which is good-practice). here's my real-world use-case to “stop” depending on browser / nodejs environment
/*
* example.js
*
* this example will stop code-excecution depending on various js-env conditions
*/
/*jslint
browser: true,
node: true,
*/
(function () {
'use strict';
var local;
// run shared js-env code
(function () {
// init local
local = {};
// init modeJs
local.modeJs = (function () {
try {
return typeof navigator.userAgent === 'string' &&
typeof document.querySelector('body') === 'object' &&
typeof XMLHttpRequest.prototype.open === 'function' &&
'browser';
} catch (errorCaughtBrowser) {
return module.exports &&
typeof process.versions.node === 'string' &&
typeof require('http').createServer === 'function' &&
'node';
}
}());
console.log('hello world');
}());
switch (local.modeJs) {
// run browser js-env code
case 'browser':
console.log(document.querySelector('body'));
console.log('stop in browser js-env');
return;
// run node js-env code
case 'node':
// stop if example.js is not invoked from the command-line
if (module !== require.main) {
console.log('stop in node non-cli js-env');
return;
}
console.log('hello command-line');
console.log('stop in node js-env');
return;
}
}());
Please consider adding a
stop
command.A
stop
command tells a JS interpreter "Go back to the start of the code".When an event is triggered, he interpreter will run again, until the stop.
This can be useful in this particular case stackoverflow.com/questions/46042185/sending-a-javascript-interpreter-back-to-the-top-of-your-code .