有没有可能在不重新加载浏览器的情况下,以某种方式停止或终止JavaScript,从而阻止任何基于JavaScript的进一步执行?
我正在考虑在PHP中使用相当于exit()
的JavaScript.
有没有可能在不重新加载浏览器的情况下,以某种方式停止或终止JavaScript,从而阻止任何基于JavaScript的进一步执行?
我正在考虑在PHP中使用相当于exit()
的JavaScript.
简短回答:
throw new Error("Something went badly wrong!");
如果你想知道更多,继续阅读.
代码中的表达式debugger;
将停止页面执行,然后浏览器的开发工具将允许您查看页面冻结时的状态.
与其试图停止一切,不如让您的代码处理错误.通过谷歌阅读大约Exception
个.它们是一种聪明的方式,可以让您的代码"跳转"到错误处理过程,而无需使用乏味的if/Else块.
在阅读了它们之后,如果您认为中断整个代码绝对是唯一的 Select ,那么抛出一个除了应用程序的"根"范围之外不会被"捕获"的异常就是解决方案:
// creates a new exception type:
function FatalError(){ Error.apply(this, arguments); this.name = "FatalError"; }
FatalError.prototype = Object.create(Error.prototype);
// and then, use this to trigger the error:
throw new FatalError("Something went badly wrong!");
be sure you don't have 100 blocks that catch 102 exception; in this case modify them to rethrow your 101 exception:
catch(exc){ if(exc instanceof FatalError) throw exc; else /* current code here */ }
return;
将终止当前函数的执行流.
if(someEventHappened) return; // Will prevent subsequent code from being executed
alert("This alert will never be shown.");
注:return;
只在一个函数中起作用.
.您可能还想知道如何停止异步代码.用clearTimeout
和clearInterval
就行了.最后,要停止XHR(103)个请求,您可以使用xhrObj.abort()
方法(which is available in 104 as well).