【发布时间】:2013-03-24 00:17:38
【问题描述】:
我的理解有问题,为什么我的代码可以正常工作:)
这是我的代码的一部分:
function updateForNextLecturer(current_id, all_lecturers, progressbar)
{
$.getJSON(
'/' + root + 'custom/ajax/update_timetable_lecturer.php?id=' + all_lecturers[current_id],
function(data)
{
if (data.successfull == 0) {
$.stickr({note: "Error occurred.", className: "classic error"});
throw new Error("error");
}
else {
percent = Math.round((current_id + 1) * 100.0 / all_lecturers.length);
progressbar.progressbar({value: percent});
if (current_id + 1 < all_lecturers.length)
updateForNextLecturer(current_id + 1, all_lecturers, progressbar);
else
$.stickr({note: "Success", className: "classic"});
}
}
);
}
需要此代码来更新所有讲师的时间表。 请求是异步的,并且一个接一个地进行,因此网络服务器负载不重。
为了使请求一个接一个地进行,我需要使用递归回调(我想这是唯一的解决方案)。问题是 - 为什么它可以正常工作,难道没有像 PHP 那样的最大嵌套级别吗? 在PHP中你会得到以下错误
Fatal error: Maximum function nesting level of '100' reached, aborting!
我用上千个请求测试了这个函数,所以嵌套层数大约是几千,但没有出现错误。是因为 JavaScript 中根本没有嵌套限制,还是因为我什么都不懂?
提前致谢。
【问题讨论】:
-
虽然这个特定示例不会溢出堆栈,因为异步回调总是在新堆栈上执行,there is a stack size limit that varies by browser。
标签: php javascript ajax recursion