如何防止JavaScript中的循环导致浏览器或应用程序崩溃? [英] How to prevent loops in JavaScript that crash the browser or Apps?

查看:523
本文介绍了如何防止JavaScript中的循环导致浏览器或应用程序崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaScript在Windows 8.1 App中创建实时编辑器。几乎完成了,但问题是每当我运行这样的错误循环或函数然后它会自动挂起或退出。

I am creating a live editor in Windows 8.1 App using JavaScript. Almost done with that, but the problem is whenever I run such bad loops or functions then it automatically hangs or exits.

我用一个循环测试它,如:(它只是一个例子 - 用户可以用自己的方式编写循环..)

I test it with a loop such as:( It just a example-user may write its loop in its own way..)

for(i=0;i<=50000;i++)
{
   for(j=0;j<5000;j++){
     $('body').append('hey I am a bug<br>');
   }
}

我知道这是任何应用程序的最差条件或浏览器来处理这种循环。所以在这里我希望如果用户使用这样的循环然后我如何处理它,以产生他们的输出?

I know that this is a worst condition for any app or browser to handle that kind of loop. So here I want that if user uses such a loop then how I handle it, to produce their output?

或者,如果它不能保护我的应用程序的那种循环,如果它对我的应用程序是危险的,所以我提醒用户:

Or if its not possible to protect my app for that kind of loop, if it is dangerous to my app so I alert the user that:


运行此代码段可能会导致应用程序崩溃!

Running this snippet may crash the app!

我有一个如果代码有类似的东西(i = 0; i< = 5000; i ++)那么上面的警告会显示,怎么办?一个正则表达式?

I have an idea to check the code by using regular expressions if code have something like for(i=0;i<=5000;i++) then the above alert will show, how to do a Regex for that?

也可以将C#作为后端

推荐答案

我有2个解决方案:



1。



我的第一个解决方案是定义变量
startSeconds = new Date()。getSeconds();

然后,使用正则表达式,我在嵌套循环中插入这段代码。

Then, using regex, I'm inserting this piece of code inside the nested loop.

;if(startSecond < new Date().getSeconds())break;

所以,它的作用是每次循环运行时,它会做两件事:

So, what it does is each time the loop runs, it does two things:

检查startSecond是否小于当前秒 new Date()。getSeconds();

Checks if startSecond is less than current seconds new Date().getSeconds();.

例如,startSecond可能是22. new Date()。getSeconds()可能返回24.Now, if 条件成功所以它打破了循环。

For example, startSecond may be 22. new Date().getSeconds() may return 24.Now, the if condition succeeds so it breaks the loop.

大多数情况下,一个非危险的循环应该运行大约2到3秒

Mostly, a non dangerous loop should run for about 2 to 3 seconds

(var i = 0; i< 30; i ++){} 的小循环将运行完全,但大循环将运行3至4秒,这是完全可以的。

Small loops like for(var i=0;i<30;i++){} will run fully, but big loops will run for 3 to 4 seconds, which is perfectly ok.

我的解决方案使用您自己的50000 * 5000示例,但它不会崩溃!

My solution uses your own example of 50000*5000, but it doesn't crash!

现场演示: http:// jsfiddle .net / nHqUj / 4

Live demo:http://jsfiddle.net/nHqUj/4

我的第二个解决方案是定义两个变量 start max

My second solution would be defining two variables start, max.

Max应该是您愿意运行的最大循环数。例子1000。

Max should be the maximum number of loops that you are willing to run. Example 1000.

然后,使用正则表达式,我在嵌套循环中插入这段代码。

Then, using regex, I'm inserting this piece of code inside the nested loop.

;start+=1;if(start>max)break;

所以,它的作用是每次循环运行时,它会做两件事:

So, what it does is each time the loop runs, it does two things:


  1. start 的值增加1。

检查start是否大于 max 。如果是,它会打破循环。

Checks whether start is greater than the max. If yes, it breaks the loop.

此解决方案也使用您自己的50000 * 5000示例,但它没有'崩溃!

This solution also uses your own example of 50000*5000, but it doesn't crash!

更新的演示: http: //jsfiddle.net/nHqUj/3

Updated demo:http://jsfiddle.net/nHqUj/3

正在使用的正则表达式:(?:( for |而|做)\s * \([^ \ {\}] * \))\s * \ {([^ \ {\}] +)\}

Regex I'm using:(?:(for|while|do)\s*\([^\{\}]*\))\s*\{([^\{\}]+)\}

这篇关于如何防止JavaScript中的循环导致浏览器或应用程序崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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