JavaScript 调试

发布于 2024-09-24 08:39:44 字数 225 浏览 6 评论 0原文

我最近开始修补 Project Euler 问题,并尝试用 Javascript 来解决它们。这样做我往往会产生许多无限循环,现在我想知道是否有比杀死 Firefox 或 Chrome 中的选项卡更好的方法来终止脚本?

另外,firebug 是否仍然被认为是“最好的”调试器(我自己看不出 firebug 和 safari/chrome 中的 web 开发工具之间有多大区别)。

无论如何,祝你周日愉快!

I have recently started to tinker with Project Euler problems and I try to solve them in 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?

Also, is firebug still considered the "best" debugger (myself I can't see much difference between firebug and web dev tool in safari/chrome ).

Any how have a nice Sunday!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

成熟的代价 2024-10-01 08:39:44

Firebug 仍然是我个人选择的工具。

至于杀死无限循环的方法。有些浏览器会完全阻止这种情况发生。但是,我仍然更喜欢直接使用 ctrl + w,但这仍然会关闭选项卡。

您可以考虑其他一些替代方案:

歌剧:蜻蜓

Safari / Chrome:网络检查器

不过,Opera 有一套很好的开发工具,我发现它们非常有用。 (工具->高级->开发者工具)

Firebug is still my personal tool of choice.

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.

Some of the other alternatives you can look into:

Opera : Dragonfly

Safari / Chrome : Web Inspector

Although, Opera has a nice set of developer tools which I have found pretty useful. (Tools->Advanced->Developer Tools)

何其悲哀 2024-10-01 08:39:44

如果您不想放入代码来显式退出,请尝试使用条件断点。如果您打开 Firebug 的脚本控制台并右键单击代码旁边的装订线,它将插入一个断点,并为您提供一个选项来触发断点满足某些条件。例如,如果您的代码是这样的:

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

...您可以等待循环的所有 10,000 次迭代完成,或者可以在循环内的某个位置放置一个条件断点并指定条件 intMaxIterations intMaxIterations intMaxIterations intMaxIterations intMaxIterations intMaxIterations 9000。这将允许循环内的代码运行 1000 次(实际上是 1001 次)。此时,如果您愿意,可以刷新页面。

但是,一旦脚本进入无限循环(无论是由于错误还是设计),如果您还没有为此做好准备,那么据我所知,您无能为力来阻止它继续进行。这通常就是为什么当我进行大量递归操作时,我会对特定代码块的运行次数进行限制。有很多方法可以做到这一点。如果您认为该行为是一个实际错误,请考虑抛出它。例如

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

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

编辑:
我突然想到,因为您是唯一使用此脚本的人,所以 Web Workers 是理想的解决方案。

您看到的基本问题是,当 JS 进入无限循环时,它会阻塞浏览器,使其无法响应您通常用于停止执行的任何事件。 Web Worker 仍然一样快,但它们让您的浏览器减轻负担并且事件正常触发。这个想法是,您将高要求任务(在本例中为欧拉问题算法)传递给 Web Worker JS 文件,该文件在自己的线程中执行,并且仅在主浏览器不需要时才消耗 CPU 资源。最终结果是您的 CPU 仍然像现在一样飙升,但您的浏览器保持快速和响应。

第一次设置网络工作者有点麻烦,但在这种情况下你只需要做一次。如果您的算法永远不会返回,只需按一下按钮并终止工作线程即可。有关详细信息,请参阅 MDC 上的使用 Web Workers

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. 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. This will allow the code inside the loop to run 1000 times (well, actually 1001 times). 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. E.g.

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.

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. Web workers are still just as fast, but they leave your browser unburdened and events fire normally. 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. The net result is that your CPU still spikes like it does now, but your browser stays fast and responsive.

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.

木格 2024-10-01 08:39:44

虽然拥有 Firebug 或 webkit 调试器很好,但浏览器对于 Project Euler 来说似乎是开销。为什么不使用 RhinoV8

While having Firebug or the webkit debuggers is nice, a browser otherwise seems like overhead for Project Euler stuff. Why not use a runtime like Rhino or V8?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文