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 Entry
After 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.