如何测试潜在的“浏览器崩溃”情况JavaScript?

发布于 2024-09-05 14:20:03 字数 224 浏览 7 评论 0原文

我一直在用 JavaScript 解决 http://projecteuler.net/ 上的一些问题。我一直在使用一个简单的 html 页面并在脚本标签中运行我的代码,这样我就可以在浏览器的控制台中记录我的结果。当尝试循环时,我有时会导致浏览器崩溃。

有没有更好的环境让我做这样的开发?

I've been having a crack at some of the problems over at http://projecteuler.net/ with JavaScript. I've been using a simple html page and running my code in script tags so I can log my results in the browsers' console. When experimenting with loops I sometimes cause the browser to crash.

Is there a better environment for me to do this kind of development?

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

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

发布评论

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

评论(6

萧瑟寒风 2024-09-12 14:20:03
  1. 每个选项卡都有单独进程的浏览器
  2. 调试器断点
  3. ,如果达到某个时间阈值则中断循环
  1. a browser that has separate processes for each tab
  2. debugger breakpoints
  3. an if that breaks the loop if some threshold for time is hit
蹲墙角沉默 2024-09-12 14:20:03

如果您在浏览器中运行计算量大的程序,您可能需要考虑使用Web Worker。简而言之,它们允许您在不同的线程中运行代码,而不会锁定浏览器。

If you're running computationally expensive programs in your browser, you may want to look at using web workers. In short, they allow you to run code in a different thread which won't lock up the browser.

反话 2024-09-12 14:20:03

如果您只是对运行 JavaScript 程序感兴趣,为什么不使用 Node.js 甚至 Rhino 之类的东西呢?这样,您可以轻松地记录输出,而不会在遇到“麻烦”时丢失它。

If you are just interested in running javascript programs as such, why don't you use something like node.js or even Rhino? That way you can easily log output without loosing it if it get into 'trouble'.

短暂陪伴 2024-09-12 14:20:03

我可以想到两种现成的可能性:

1)使用带有断点的调试器。 Firebug 相当不错。 Safari 和 Chrome 也有一些内置的调试工具。

2) 您可以使用 Mozilla Rhino 和 Env-js 将测试移出浏览器(请参阅< a href="http://groups.google.com/group/envjs" rel="nofollow noreferrer">http://groups.google.com/group/envjs 和 http://github.com/thatcher/env-js )

I can think of two ready possibilities:

1) Use a debugger that has breakpoints. Firebug is rather nice. Safari and Chrome also have some built-in debugging tools.

2) You could move your testing out of the browser using Mozilla Rhino and Env-js (see http://groups.google.com/group/envjs and http://github.com/thatcher/env-js )

回忆凄美了谁 2024-09-12 14:20:03

如果脚本运行时间超过 5-10 秒,所有现代浏览器(Opera 除外)都应该中断脚本 (来源)。

在 Firefox 中,如果 10 秒意味着太大的惩罚,您甚至可以降低此阈值。另请注意,即使您从 Firebug 控制台运行代码,此机制也会启动:

Stop Script on Firefox http://img819.imageshack.us/img819/9655/infloopsp.jpg

我认为仅此功能就应该为这些疯狂的实验提供一个非常安全的环境:)

All modern browsers (except Opera) should interrupt your script if it runs for more than 5-10 seconds (Source).

In Firefox you can even lower this threshold, if 10 seconds mean a too big punishment. Also note that this mechanism kicks in even when you run code from the Firebug console:

Stop Script on Firefox http://img819.imageshack.us/img819/9655/infloopsp.jpg

I think this feature alone should provide a pretty safe environment for these loopy experiments :)

静待花开 2024-09-12 14:20:03

除了修复导致浏览器崩溃的错误之外,您无法采取任何措施来防止浏览器崩溃。

您至少可以通过使用像 Chrome 这样的浏览器来减轻崩溃的影响,该浏览器通常将一个选项卡中的崩溃与其他选项卡隔离(因此您只丢失自己的页面),或者只是安装专门用于测试的单独浏览器。

在跟踪可能已写入日志的数据方面,您可以使用像 Firebug 这样具有内置调试器的插件,这样您就可以在中途暂停脚本执行并检查变量(大概在发生任何崩溃之前)。

There's nothing you can do to keep the browser from crashing other than fix bugs that cause the browser to crash.

You can at least mitigate the impact of the crash by using a browser like Chrome that generally segregates crashes in one tab from the others (so you lose only your own page), or just installing a separate browser specifically for testing.

In terms of keeping track of data that might have gone to the log, you might use a plugin like Firebug that has a built-in debugger so you can pause the script execution partway through and examine your variables, presumably before any crash occurs.

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