萤火虫(1.10.1)建议JavaScript并不限于firefox(13.0)中的单一线程, [英] firebug (1.10.1) suggests javascript is not confined to a single thread in firefox (13.0)
问题描述
我创建了一个简单的示例html文档来说明我看到的异常情况。 / p>
<!DOCTYPE html>
< html>
< head>
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.jstype =text / javascript>< / script>
< / head>
< body id =main_body>
< script type =text / javascript> (),函数(){
loadStuff();
console&& amp; $ amp; amp;
$(function(){
$(。test-trigger ; console.log&& console.log(这可能会首先发生);
});
});
function loadStuff(){
$ .get(http://google.com/)
.fail(function(){
console& & console.log&& console.log(这可能会发生第二个。);
});
}
< / script>
< button class =test-trigger> test< / button>
< / body>
< / html>
如果您将此文档加载到Firefox中(我在Windows上使用版本13.0与Firebug版本1.10.1 7),单击测试,然后查看Firebug中的控制台选项卡,您应该注意到get请求失败(跨域违例,与我在这里要做的点无关),然后您很可能会看到:
这可能会首先发生。
这可能会发生第二。
现在,在第13行和第20行放置断点:$ b
$ b
13:console&& console.log&& console.log(这可能会首先发生。);
20:console&& console.log&& console.log(这可能会发生第二。);
如果再次单击测试,您将按预期中断13行。现在,恢复执行。如果你的经验和我的一样,你不会在第20行中断。另外,如果你切换到控制台选项卡,你会看到下面的日志输出序列:
pre > 这可能会发生第二个。
这可能会首先发生。
对我来说,这表明ajax请求的失败处理程序正在执行点击处理程序正在执行中。我一直以来都相信,单个页面的所有JavaScript都将由任何浏览器中的单个线程执行。我在这里错过了一些很明显的东西吗如果我使用Visual Studio调试在IE中运行的同一个页面,那么这两个断点都会按照我的预期打开。
>我认为可以肯定的是,您所观察到的异常是由Firebug如何在引擎盖下实现断点/工作引起的。我不能确认。这也发生在OS X的FF 14上。
除非jQuery立即执行你的 鉴于JavaScript的单线程特性,功能基本上是原子的;他们不会被回调中断。 看起来好像你在模拟如果 为了从等式中取出断点,下面是一个修改后的版本。 我会文件问题与Firebug。 While debugging some client side javascript today in Firefox, I ran into something that I found quite odd and little unnerving. Also, I was unable to duplicate this behavior while debugging the same script with IE / VS2010. I created a simple example html document to illustrate the anomally I am seeing. If you load this document into Firefox (I am using version 13.0 with Firebug version 1.10.1 on Windows 7), click test, and view the console tab in Firebug you should notice that the get request fails (cross domain violation that has nothing to do with the point I'm trying to make here), and then you will most likely see: Now, place breakpoints on lines 13 and 20: If you click test again you will break on line 13 as expected. Now, resume execution. If your experience is like mine, you will not break on line 20. Also if you switch to the console tab you will see the following sequence of log output: To me, this suggests that the fail handler of the ajax request is being executed in a thread other than that which the click handler is being executed in. I have always been led to believe that all the javascript for a single page will be executed by a single thread in any browser. Am I missing something really obvious here? Thanks for any insight on this observation. Oh, if I debug the same page running in IE using Visual Studio, both breakpoints are hit as I would expect. I think it's safe to assume that the anomaly you're observing is caused by how Firebug implements breakpoints/works under the hood. I can't confirm that though. This also happens with FF 14 on OS X. Unless jQuery immediately executes your Given the single threaded nature of JavaScript, functions will be essentially atomic; they will not get interrupted by a callback. It seems as though you're trying to simulate what would happen if the To take breakpoints out of the equation, here's a modified version. The rest of the markup is unchanged. The I'd file an issue for this with Firebug. 这篇关于萤火虫(1.10.1)建议JavaScript并不限于firefox(13.0)中的单一线程,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! fail()
函数并超过整个 XMLHttpRequest
对象,那么就是保证语句的排序是,这可能会首先发生。 / code>然后
这可能会发生第二次。
。
点击
函数需要一段时间才能完成在调用 loadStuff()
之后执行。 点击
函数不应该被执行的失败
方法中断(mind ==吹来说,你找到了一种方法来实现这一点)。
$(function(){
$(。test-trigger ).on(click,function(){
loadStuff();
for(var i = 0; i <1000000000; i ++)
{
// block一些有趣的计算或
console&& console.log&& amp; console.log(这可能会先发生);
});
});
function loadStuff(){
$ .get(http://google.com/)
.fail(function(){
console& & console.log&& console.log(这可能会发生第二个。);
});
$ / code> 点击
函数在调用 loadStuff()
之后显然需要很长时间才能执行,但控制台仍然会在这里反映日志语句的正确顺序。另外值得注意的是,如果你插入相同的断点,那么排序将是无效的,就像原来的例子。
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js" type="text/javascript" ></script>
</head>
<body id="main_body">
<script type="text/javascript">
$(function () {
$(".test-trigger").on("click", function () {
loadStuff();
console && console.log && console.log("this will probably happen first.");
});
});
function loadStuff() {
$.get("http://google.com/")
.fail(function () {
console && console.log && console.log("this will probably happen second.");
});
}
</script>
<button class="test-trigger">test</button>
</body>
</html>
this will probably happen first.
this will probably happen second.
13: console && console.log && console.log("this will probably happen first.");
20: console && console.log && console.log("this will probably happen second.");
this will probably happen second.
this will probably happen first.
fail()
function and surpasses the whole XMLHttpRequest
object, then there is a guarantee that the ordering of the statements will be this will probably happen first.
then this will probably happen second.
.click
function takes a while to finish executing after calling loadStuff()
. The click
function shouldn't get interrupted by the fail
method executing (mind == blown that you found a way to make that happen).$(function () {
$(".test-trigger").on("click", function () {
loadStuff();
for (var i = 0; i < 1000000000; i++)
{
//block for some interesting calculation or something
}
console && console.log && console.log("this will probably happen first.");
});
});
function loadStuff() {
$.get("http://google.com/")
.fail(function () {
console && console.log && console.log("this will probably happen second.");
});
}
click
function clearly takes a long time to execute, after calling loadStuff()
, but the console will still reflect the correct order of the log statements here. Also worth noting, if you insert the same breakpoints, the ordering will be invalid, like the original example.