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

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


当前回答

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

thisFunctionDoesNotExistAndWasCreatedWithTheOnlyPurposeOfStopJavascriptExecutionOfAllTypesIncludingCatchAndAnyArbitraryWeirdScenario();

或者像这样:

new new

其他回答

我做的事:

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

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

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

thisFunctionDoesNotExistAndWasCreatedWithTheOnlyPurposeOfStopJavascriptExecutionOfAllTypesIncludingCatchAndAnyArbitraryWeirdScenario();

或者像这样:

new new

类似这样的方法可能有用:

function javascript_abort()
{
   throw new Error('This is not an error. This is just to abort javascript');
}

从这里开始:

http://vikku.info/codesnippets/javascript/forcing-javascript-to-abort-stop-javascript-execution-at-any-time/

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

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

我在用

返回错误;

如果我想中止JavaScript从进一步向下运行。