In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to detect and prevent JavaScript endless cycle", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to detect and prevent the JavaScript endless cycle"!
Preface
How is the Js endless cycle caused? In fact, if we don't pay attention or omit when we write the code, we will write an endless loop, such as the following code:
If iTunes + is not written, this will be an endless cycle. If you want to know the consequences of the endless cycle, try it yourself.
A dead loop is a piece of JavaScript code that mistakenly prevents the termination of a loop condition.
An infinite loop is dangerous because it can cause the environment in which you are running your code (browser or NodeJS server) to crash or freeze your computer, causing it to stop responding.
For and while statements are common causes of an infinite loop, so this tutorial will help you understand how to detect and fix the resulting infinite loop of statements.
Let's start by fixing the infinite loop in the for statement.
Fix an infinite loop in a for statement
A for statement may cause an infinite loop when you mistake the second expression into the assignment operator rather than a comparison operator (in the conditional expression)
The following is an example of a statement that for can cause an infinite loop. Notice how the second expression is I = 10:
For (let I = 0; I = 10; iTunes +) {console.log ("Infinite loop");}
The for statement requires the second expression to evaluate false to terminate the loop. In the above example, the assignment operator I = 10 is placed as a conditional expression instead of like I
< 10or之类的比较运算符i >ten.
Because the assignment operator always evaluates to true, the for statement does not stop printing "Infinite loop" to the console, which may cause your computer to panic.
To fix the statement above for, you need to replace the second expression with what the for statement can achieve. One example is the use of the comparison operator (=)
/ / the second expression is replaced with 0; iTunes +) {console.log ("Infinite loop");}
As you can see from the above code, as long as the variable I is greater than, the loop continues to be 0.
Because the value I of is already greater than the value of 0 at initialization, the second expression will always evaluate to true, resulting in an infinite loop.
To fix the above code, the third expression must decrease I rather than increase its value:
For (let I = 5; I > 0; iMury -) {console.log ("Infinite loop");}
Another example of a statement where for can cause an infinite loop is to omit all three expressions in parentheses, as follows:
For (;) {console.log ("Infinite loop");}
But the above code can only be written on purpose, so you shouldn't use it in your project unless you want your computer to crash.
Although for the statement you write will certainly be more complex than the example above, you can still use the same principles to find and fix errors in the statement.
First, you need to make sure that the second expression you put in the for statement actually evaluates to false. Exe.
If the second expression is correct, start checking the first and third expressions. Does the first expression initialize a value that true always evaluates to when executing the second expression?
Finally, does the third expression correctly increment or decrement the value of the variable initialized in the first expression?
To sum up: first check your second expression, and then check the first and third expressions sequentially.
Next, let's learn how to fix the infinite loop caused by the while statement.
Fix an infinite loop in a while statement
A while statement can cause an infinite loop when the conditional expression parentheses are always evaluated with true:
While (true) {console.log ("Infinite loop");}
To prevent an infinite loop, the conditional expression of the while statement must be able to evaluate to false.
One of the most common mistakes when writing while statements is to forget to modify the value of the variable used for conditional expressions.
Notice how the value of never is changed in the following example:
Let I = 0; while (I < 6) {console.log ("Infinite loop");}
You need to increase the value inside the statement body I, while so that when false executes the loop, the conditional expression evaluates to:
Let I = 0; while (I < 6) {console.log ("Infinite loop"); iTunes;}
No matter how complex your while statement is, you need to make sure that the conditional expression while () in parentheses evaluates to false.
If you still cannot find the cause of the infinite loop, you can use this console.log () statement to print the value of the variable used by the conditional expression:
Let I = 0; while (I < 6) {console.log ("Infinite loop"); iTunes; console.log (I);}
Console logs can help you identify the wrong line of code and fix it.
At this point, I believe you have a deeper understanding of "how to detect and prevent the JavaScript dead cycle". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.