简体   繁体   English

Javascript调试

[英]Javascript debugging

I have recently started to tinker with Project Euler problems and I try to solve them in Javascript. 我最近开始修补Project Euler问题,我尝试用Javascript解决它们。 Doing this I tend to produce many endless loops, and now I'm wondering if there is any better way to terminate the script than killing the tab in Firefox or Chrome? 这样做我倾向于产生许多无限循环,现在我想知道是否有更好的方法来终止脚本而不是杀死Firefox或Chrome中的选项卡?

Also, is firebug still considered the "best" debugger (myself I can't see much difference between firebug and web dev tool in safari/chrome ). 另外,firebug仍然被认为是“最好的”调试器(我自己在safari / chrome中看不到firebug和web dev工具之间的差异)。

Any how have a nice Sunday! 星期天怎么样!

Firebug is still my personal tool of choice. Firebug仍然是我个人的首选工具。

As for a way of killing your endless loops. 至于杀死无尽循环的方法。 Some browsers will prevent this from happening altogether. 有些浏览器会阻止这种情况发生。 However, I still prefer just going ctrl + w , but this still closes the tab. 但是,我仍然喜欢只使用ctrl + w ,但这仍然会关闭标签。

Some of the other alternatives you can look into: 您可以查看其他一些替代方案:

Opera : Dragonfly 歌剧:蜻蜓

Safari / Chrome : Web Inspector Safari / Chrome:Web Inspector

Although, Opera has a nice set of developer tools which I have found pretty useful. 虽然,Opera有一套很好的开发者工具,我发现它们非常有用。 (Tools->Advanced->Developer Tools) (工具 - >高级 - >开发人员工具)

If you don't want to put in code to explicitly exit, try using a conditional breakpoint. 如果您不想输入代码以显式退出,请尝试使用条件断点。 If you open Firebug's script console and right-click in the gutter next to the code, it will insert a breakpoint and offer you an option to trigger the breakpoint meets some condition. 如果您打开Firebug的脚本控制台并右键单击代码旁边的装订线,它将插入断点并为您提供触发断点符合某些条件的选项。 For example, if your code were this: 例如,如果您的代码是这样的:

var intMaxIterations = 10000;
var go = function() {
    while(intMaxInterations > 0) {
        /*DO SOMETHING*/
        intMaxIterations--;
    }
};

... you could either wait for all 10,000 iterations of the loop to finish, or you could put a conditional breakpoint somewhere inside the loop and specify the condition intMaxIterations < 9000 . ...您可以等待循环的所有10,000次迭代完成,或者您可以在循环内的某处放置条件断点并指定条件intMaxIterations < 9000 This will allow the code inside the loop to run 1000 times (well, actually 1001 times). 这将允许循环内的代码运行1000次(好吧,实际上是1001次)。 At that point, if you wish, you can refresh the page. 此时,如果您愿意,可以刷新页面。

But once the script goes into an endless loop (either by mistake or design), there's not a lot you can do that I know of to stop it from continuing if you haven't prepared for this. 但是一旦脚本进入无限循环(无论是错误还是设计),如果你还没有为此做好准备,那么我所知道的并不是很多可以阻止它继续下去。 That's usually why when I'm doing anything heavily recursive, I'll place a limit to the number of times a specific block of code can be run. 这通常是为什么当我做任何重复递归时,我会限制特定代码块的运行次数。 There are lots of ways to do this. 有很多方法可以做到这一点。 If you consider the behaviour to be an actual error, consider throwing it. 如果您认为该行为是实际错误,请考虑抛出它。 Eg 例如

var intMaxIterations = 10000;
var go = function() {
    while(true) {
        /*DO SOMETHING*/
        intMaxIterations--;

        if (intMaxIterations < 0) {
            throw "Too many iterations. Halting";
        }
    }
};

Edit: It just occurred to me that because you are the only person using this script, web workers are the ideal solution. 编辑:我刚刚想到,因为您是唯一使用此脚本的人,所以Web工作人员是理想的解决方案。

The basic problem you're seeing is that when JS goes into an endless loop, it blocks the browser, leaving it unresponsive to any events that you would normally use to stop the execution. 您看到的基本问题是,当JS进入无限循环时,它会阻止浏览器,使其无法响应您通常用于停止执行的任何事件。 Web workers are still just as fast, but they leave your browser unburdened and events fire normally. Web工作人员的速度仍然很快,但他们会让您的浏览器无负担,并且事件正常发生。 The idea is that you pass off your high-demand tasks (in this case, your Euler problem algorithm) to a web worker JS file, which executes in its own thread and consumes CPU resources only when they are not needed by the main browser. 我们的想法是将您的高需求任务(在本例中为您的Euler问题算法)传递给Web worker JS文件,该文件在其自己的线程中执行,并且仅在主浏览器不需要时才占用CPU资源。 The net result is that your CPU still spikes like it does now, but your browser stays fast and responsive. 最终结果是您的CPU仍然像现在一样高峰,但您的浏览器保持快速响应。

It is a bit of a pest setting up a web worker the first time, but in this case you only have to do it once. 第一次建立网络工作者有点害虫,但在这种情况下,你只需要做一次。 If your algorithm never returns, just hit a button and kill the worker thread. 如果你的算法永远不会返回,只需点击一个按钮就可以杀死工作线程。 See Using Web Workers on MDC for more info. 有关详细信息,请参阅在MDC上使用Web Workers

While having Firebug or the webkit debuggers is nice, a browser otherwise seems like overhead for Project Euler stuff. 虽然使用Firebug或webkit调试器很好,但浏览器看起来似乎是Project Euler的开销。 Why not use a runtime like Rhino or V8 ? 为什么不使用像RhinoV8这样的运行时?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM