Javascript-js recursive function, run more than 500 times to report stack overflow error?

Source: Internet
Author: User

1.PHP development, there is a need for more than 1700 XML file data to be read and stored in the database. My method is that the front end is recursive with the JS function, one commit through Ajax at a time. Now the problem is JS recursive function ran about 500 times, the browser will report stack overflow error, stop running.
2. Front-end Recursive functions:
function Update_hotelinfo_queue (data,i)

    {        if(i==(data.length-1))        {            return;        }        $("#processInfo").html("正在处理第"+(i+1)+"个城市数据
"); var url="__CONTROLLER__/updateHotelInfo"; 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. This error is the browser to determine the current recursion is a dead loop?
4. Supplementary error information

JS Small white ask you ~ ~

Reply content:

1.PHP development, there is a need for more than 1700 XML file data to be read and stored in the database. My method is that the front end is recursive with the JS function, one commit through Ajax at a time. Now the problem is JS recursive function ran about 500 times, the browser will report stack overflow error, stop running.
2. Front-end Recursive functions:
function Update_hotelinfo_queue (data,i)

    {        if(i==(data.length-1))        {            return;        }        $("#processInfo").html("正在处理第"+(i+1)+"个城市数据
"); var url="__CONTROLLER__/updateHotelInfo"; 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. This error is the browser to determine the current recursion is a dead loop?
4. Supplementary error information

JS Small white ask you ~ ~

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

The advantage of recursive functions is that they make the code simple and do more with less code.

But there is a big drawback is to take up memory, we know that each call function will consume a portion of memory, called 进栈 , after the function is finished, memory release, called 出栈 .

Recursive function every recursion, all rely on the next recursive results to output, so that the function has been in the stack, no stack, memory has been occupied, not released in time.

So your error is rightfully right. Stack overflow translation is the stack overflow.

The workaround is simple, and it is recommended to use loops, so that each time the function executes, it automatically frees the memory

The recursive algorithm is adjusted to realize the recursive tail call

You don't have to recursion, use loops. Tail recursion to JS and useless, and no optimization seems.

Ajax relies on callbacks that need to use promise-defferd, but more than 1700 data, are you sure you want to use this method?

If your previous method is recursive, see if it can be done in batches.

Do not use recursion where you can use the loop. Recursion is used to solve the problem of whether or not the loop algorithm is otherwise, such as 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.