javascript - js错误Uncaught RangeError: Maximum call stack size exceeded
PHPz
PHPz 2017-04-10 15:56:44
[JavaScript讨论组]

首先调用get_content()方法,然后在get_content方法中调用handle_content()方法,然后在handle_content方法中调用post_content()方法,最后在post_content方法中再调用get_content()方法,不知到该怎样避免超出内存呢?

function get_content()
{
    $.ajax({
        url:'/air_ajax.php',
        type:'get',
        dataType:'json',
        timeout:1000,

        async:false,
        success:function(json)
        {
            if(json.status)
            {
                handle_content(json.article);
            }
            else
            {
                console.log('get article none');
            }
        }
    });
}

function handle_content(article)
{
    if(!article){
        console.log('article is illegal');
        console.log(article);
        return;
    }
    var $box=$('<p class="wrap">'+article.content+'</p>');
    var $children=$box.find('#content').children();
    if($children.length>0)
    {
        $children.each(function()
        {
            //console.log('hello');
            var tag=this.nodeName.toLowerCase();

             if(this.nodeType==1 && tag!=='p' && tag!=='p')
             {
             var html=$(this).html();
             //console.log(this);
             $(this).replaceWith('<p>'+html+'</p>');
             }

        });
    }


    $box.find('img').each(function()
    {
        $(this).attr('src','/img/load_common.gif');
    });
    if($box.find('#content').length>0)
    {
        var html=$box.find('#content').html();
    }
    else
    {
        var html=$box.html();
    }
    //var $html=$('<p>'+html+'</p>');
    ue.setContent(html);
    ue.execCommand('selectall');
    ue.execCommand('removeformat');
    var content=ue.getContent();
    var $html=$('<p>'+content+'</p>');
    $html.find('img').each(function()
    {
        var src=$(this).attr('src');
        var datasrc=$(this).attr('src');
        $(this).removeAttributes();
        $(this).attr('src',src).attr('src',datasrc);
    });
    $html.find('a').remove();
    var html=$html.html();
    data={id:article.id,content:html};
    
    post_content(data);
}
**var count;**
function post_content(data)
{
    **count++;**
    $.ajax({
        url:'/air_ajax.php',
        type:'post',
        dataType:'json',
        timeout:1000,
        async:false,
        data:data,
        success:function(json)
        {
            if(json.status)
            {
                console.log('process success '+id);
                **if(count>400)
                {
                    setTimeout(get_content,5000);
                    count=0;
                }
                else
                {
                    get_content();
                }**
                

            }
            else
            {
                console.log(json.errorInfo);
                status=false;
            }
        }
    });
}
PHPz
PHPz

学习是最好的投资!

全部回复(2)
高洛峰

这是无限递归呐。。没有终止条件肯定会爆栈啊

建议把 get_content()改成setTimeout(get_content, 0),使之成为异步调用,自然就瓦解了递归栈

ringa_lee

题主是想将get的数据处理后post,递归唯一条件是json.status 然而递归运行不能中止导致了超出stack. post_content()内部调用的是get_content() 两者没有数据上的联系因此不必用递归的方式直接循环就可以做到

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号