是否有可能以某种方式停止或终止JavaScript,以防止任何进一步的基于JavaScript的执行发生,而无需重新加载浏览器?

我正在考虑在PHP中等价于exit()的JavaScript。


当前回答

我知道这很老了,但我想这么做,我发现了,在我看来,一个稍微改进的投掷答案的解。只是暂时抑制错误消息,然后使用setTimeout重新激活它们:

setTimeout(function() {
    window.onerror = function(message, url, lineNumber) {  
        return false;
    };
}, 50); // sets a slight delay and then restores normal error reporting
window.onerror = function(message, url, lineNumber) {  
    return true;
};
throw new Error('controlledError');

其他回答

我知道这很老了,但我想这么做,我发现了,在我看来,一个稍微改进的投掷答案的解。只是暂时抑制错误消息,然后使用setTimeout重新激活它们:

setTimeout(function() {
    window.onerror = function(message, url, lineNumber) {  
        return false;
    };
}, 50); // sets a slight delay and then restores normal error reporting
window.onerror = function(message, url, lineNumber) {  
    return true;
};
throw new Error('controlledError');

如果你在一个函数中,你可以使用return退出它;但这不会停止调用该函数的父函数的执行。

我做的事:

setTimeout(function() { debugger; }, 5000)

这样我就有5秒钟的时间与UI交互,然后停止。最后一次我使用的是当我需要离开自定义工具提示可见,做一些样式变化。

在JavaScript函数中定义一个变量,如果你想执行这个函数,将这个变量设置为1,如果你想停止它,将它设置为0

var execute;
function do_something()
{
if (execute == 1)
{
// execute your function
}
else
{
 // do nothing
}
}

你可以做一个JavaScript错别字:D(跳出框框思考)

thisFunctionDoesNotExistAndWasCreatedWithTheOnlyPurposeOfStopJavascriptExecutionOfAllTypesIncludingCatchAndAnyArbitraryWeirdScenario();

或者像这样:

new new