js错误Uncaught RangeError: Maximum call stack size exceeded

首先调用get_content()方法,然后在get_content方法中调用handle_content()方法,然后在handle_content方法中调用post_content()方法,最后在post_content方法中再调用get_content()方法,不知到该怎样避免超出内存呢?
functionget_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('getarticlenone');
}
}
});
}
functionhandle_content(article)
{
if(!article){
console.log('articleisillegal');
console.log(article);
return;
}
var$box=$(''+article.content+'
');
var$children=$box.find('#content').children();
if($children.length>0)
{
$children.each(function()
{
//console.log('hello');
vartag=this.nodeName.toLowerCase();
if(this.nodeType==1&&tag!=='div'&&tag!=='p')
{
varhtml=$(this).html();
//console.log(this);
$(this).replaceWith('

'+html+'

');
}
});
}
$box.find('img').each(function()
{
$(this).attr('src','/img/load_common.gif');
});
if($box.find('#content').length>0)
{
varhtml=$box.find('#content').html();
}
else
{
varhtml=$box.html();
}
//var$html=$('
'+html+'
');
ue.setContent(html);
ue.execCommand('selectall');
ue.execCommand('removeformat');
varcontent=ue.getContent();
var$html=$('
'+content+'
');
$html.find('img').each(function()
{
varsrc=$(this).attr('src');
vardatasrc=$(this).attr('data-src');
$(this).removeAttributes();
$(this).attr('src',src).attr('data-src',datasrc);
});
$html.find('a').remove();
varhtml=$html.html();
data={id:article.id,content:html};
post_content(data);
}
**varcount;**
functionpost_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('processsuccess'+id);
**if(count>400)
{
setTimeout(get_content,5000);
count=0;
}
else
{
get_content();
}**
}
else
{
console.log(json.errorInfo);
status=false;
}
}
});
}
MYYA
浏览 827回答 2
2回答

翻阅古今

这是无限递归呐。。没有终止条件肯定会爆栈啊建议把get_content()改成setTimeout(get_content,0),使之成为异步调用,自然就瓦解了递归栈
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript