Str*_*rae 4 javascript ajax recursion jquery loops
我有一个连续5个步骤的程序,在我的页面中,我为每个步骤调用ajax方法.
想法是运行第一个,如果一切都好,第二个,依此类推.
我的代码是:
$("#foo").click(function(){
$.ajax({
url: 'ajax.php',
async: false,
data: {
step: 1
},
dataType: 'json',
type: 'GET',
success: walk_through(data)
});
});
function walk_through(data)
{
if(data.status == 'ok')
{
if(data.next_step == 'end')
{
// All steps completed
}
else
{
// Current step completed, run next one
$.ajax({
url: 'ajax.php',
async: false,
data: {
step: data.next_step
},
dataType: 'json',
type: 'GET',
success: walk_through(data)
});
}
}
else
{
alert("Error.");
console.log(data);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到"太多的递归"错误,即使我的ajax调用被设置为同步..为什么?
更改
success: walk_through(data)
至
success: walk_through
您希望函数walk_through成为成功处理程序,但您不希望立即调用该函数.