Is the Stackoverflow error reported after running the javascript-js recursive function for more than 500 times?

Source: Internet
Author: User
1. PHP development, there is a need to read more than 1700 xml file data and save it to the database. My method is to recursively use the js function at the front end and submit it at a time through AJAX. The problem is that js recursive functions run about 500 times, and the browser reports the Stackoverflow error and stops running. 2 ....

1. PHP development, there is a need to read more than 1700 xml file data and save it to the database. My method is to recursively use the js function at the front end and submit it at a time through AJAX. The problem is that js recursive functions run about 500 times, and the browser reports the Stack overflow error and stops running.
2. front-end recursive functions:
Function update_hotelInfo_queue (data, I)

{If (I = (data. length-1) {return ;}$ ("# processInfo" ).html ("processing City data" + (I + 1) +"
"); Var url =" _ CONTROLLER _/update1_info "; var cityID = data [I]; console. log (I + "=>" + cityID); $. ajax ({url: url, cache: false, async: false, dataType: "json", type: "POST", data: {cityID: cityID}, success: function () {update_hotelInfo_queue (data, I + 1 )}})}

3. is this error because the browser determines whether the current recursion is an endless loop?
4. supplemental error message

Js tips ~~

Reply content:

1. PHP development, there is a need to read more than 1700 xml file data and save it to the database. My method is to recursively use the js function at the front end and submit it at a time through AJAX. The problem is that js recursive functions run about 500 times, and the browser reports the Stack overflow error and stops running.
2. front-end recursive functions:
Function update_hotelInfo_queue (data, I)

{If (I = (data. length-1) {return ;}$ ("# processInfo" ).html ("processing City data" + (I + 1) +"
"); Var url =" _ CONTROLLER _/update1_info "; var cityID = data [I]; console. log (I + "=>" + cityID); $. ajax ({url: url, cache: false, async: false, dataType: "json", type: "POST", data: {cityID: cityID}, success: function () {update_hotelInfo_queue (data, I + 1 )}})}

3. is this error because the browser determines whether the current recursion is an endless loop?
4. supplemental error message

Js tips ~~

"Tail call optimization" http://www.ruanyifeng.com/blo...

The advantage of recursive functions is to make the code concise and do more with less code.

However, there is a major drawback: memory usage. we know that each function call consumes part of the memory calledStack EntryAfter the function is executed, the memory is released, calledOutbound stack.

Every recursion of a recursive function depends on the result of the next recursion before it can be output. in this way, the function is always pushed to the stack without going out of the stack. the memory is always occupied and not released in time.

Therefore, the stack overflow translation means stack overflow.

The solution is simple. we recommend that you use a loop. in this way, the memory will be automatically released after the function is executed independently.

Adjust the recursive algorithm to implement recursive tail call

You don't need recursion at all. use a loop. Tail recursion is useless to js and does not seem to be optimized.

The ajax dependency callback requires the use of promise-defferd, but more than 1700 pieces of data, are you sure you want to use this method?

If you use your previous method, that is, recursion, you can see if batch execution can be performed.

Do not use recursion where loops can be used. Recursion is used to solve the problems of whether or not to use a loop algorithm, such as the Tower of Hanoi. The cost of recursion is stackoverflow.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.