当遇到无效行时,javascript执行是否会停止? [英] Does javascript execution stop when an invalid line is encountered?
问题描述
如果浏览器在网页上执行JavaScript并且遇到无效的JavaScript
(例如somerandomthingy;)此时JavaScript的执行是否停止,或者异步操作是否继续。此外,JavaScript事件仍然有效(例如onclick)。
If the browser is executing JavaScript on a web page and it encounters invalid JavaScript (e.g. somerandomthingy;) Does execution of JavaScript stop at that point, or do async operations continue. Also do JavaScript events still work (e.g. onclick).
推荐答案
是的,除了异步事件。 http://jsfiddle.net/pimvdb/R4dfJ/3/
Yes, except asynchronous ones. http://jsfiddle.net/pimvdb/R4dfJ/3/
DIY:
var a = 1, b, c;
setTimeout(function() {
b = 2;
console.log('Running', a, b, c);
}, 1000);
somerandomthingy;
c = 3;
未捕获的ReferenceError:somerandomthingy未定义
Uncaught ReferenceError: somerandomthingy is not defined
运行1 2未定义
所以:
-
a
且b
已设定 -
c
尚未设置 - 超时仍然发生
a
andb
have been setc
has not been set- Timeout still occurs
至于'on onclick仍然有用' - 好吧,如果处理程序是在运行的代码中设置的(即在错误之前),那么;如果它是在错误代码之后放置的,它将不会绑定处理程序,因此事件将不起作用。
As for 'does onclick still work' - well, if the handler is set in code that runs (i.e. before an error) it does; in case it is put after error code it won't bind the handler so events won't work.
这篇关于当遇到无效行时,javascript执行是否会停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!