你怎么能调试JavaScript它挂在浏览器? [英] How can you debug JavaScript which hangs the browser?

查看:138
本文介绍了你怎么能调试JavaScript它挂在浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个相当大的丰富网页JavaScript应用。出于某种原因,最近的变化是导致它挂随机浏览器。

I'm working on a substantially large rich web page JavaScript application. For some reason a recent change is causing it to randomly hang the browser.

我怎样才能缩小问题出在哪里?由于浏览器变得反应迟钝,我看不出有什么错误,并在下次用Firebug不能打破。

How can I narrow down where the problem is? Since the browser becomes unresponsive, I don't see any errors and can't Break on next using FireBug.

推荐答案

,你应该使用调试控制台日志

您应该使用现今大多数浏览器提供的的console.log()的功能,或者如果您使用不支持浏览器效仿。

You should use the console.log() functionality provided by most modern browsers, or emulate it if you use a browser that doesn't support it.

每次通话时间的console.log('sometext'),在浏览器的调试控制台的日志条目将与您指定的文本创建,通常后跟实际的时间。

Every time you call console.log('sometext'), a log entry in the debug console of your browser will be created with your specified text, usually followed by the actual time.

您应该设置你的应用程序流程的每个关键部分之前的日志条目,然后越来越多,直到你找到下不被叫什么日志。这意味着,日志之前的code挂浏览器。

You should set a log entry before every critical part of your application flow, then more and more until you find down what log isn't being called. That means the code before that log is hanging the browser.

您还可以使用更多功能,写自己的个人日志功能,比如一些特殊变量的状态,或包含你的程序流程等更详细的方面的目的。

You can also write your own personal log function with added functionalities, for example the state of some particular variable, or an object containing a more detailed aspect of your program flow, etc.

示例

console.log('step 1');

while(1) {}

console.log('step 2');

无限循环将停止该程序,但你仍然可以看到控制台日志记录与直到程序暂停前的程序流程。

The infinite loop will halt the program, but you will still be able to see the console log with the program flow recorded until before the program halted.

因此​​,在这个例子中,你将不会看到在控制台日志第2步,但你会看到第1步。这意味着步骤1和步骤2之间停止该程序。

So in this example you won't see "step 2" in the console log, but you will see "step 1". This means the program halted between step 1 and step 2.

您可以再添加额外的控制台登录这两个步骤之间的code为问题深感搜索,直到你找到它。

You can then add additional console log in the code between the two steps to search deeply for the problem, until you find it.

这篇关于你怎么能调试JavaScript它挂在浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆