如果我递归调用ajax函数会怎样?它可能会引起任何问题. [英] what will happen if i call ajax function recursively? it may cause any issues.
本文介绍了如果我递归调用ajax函数会怎样?它可能会引起任何问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<script>
$(document).ready(function(){
ajaxcall();
});
function ajaxcall(){
$.ajax({
url: 'test.php',
success: function(data) {
data = data.split(':');
$('#hours').html(data[0]);
$('#minutes').html(data[1]);
$('#seconds').html(data[2]);
ajaxcall();
}
});
}
</script>
<span id="hours">0</span>:<span id="minutes">0</span>:<span id="seconds">0</span>
在此代码中,我将ajax函数称为onsuccess,因此它将是无限循环.会引起什么问题吗?
In this code i called the ajax function onsuccess, so it will be infinite loop. It cause any problems?
推荐答案
这将导致您的浏览器最终用尽内存并崩溃,因为每个调用都将保存在调用堆栈中.错了;回调是异步的,因此控件将离开父函数.如果您要更新时间,建议改用以下方法:
This will cause your browser to run out of memory eventually and crash because each call will be saved on the call-stack. I was wrong; the callback is asynchronous and so control will leave the parent function. If you're trying to update the time, I recommend doing something like this instead:
$(document).ready(function(){
setInterval(ajaxcall, 1000); //calls ajaxcall every 1000 microseconds
});
function ajaxcall(){
$.ajax({
url: 'test.php',
success: function(data) {
data = data.split(':');
$('#hours').html(data[0]);
$('#minutes').html(data[1]);
$('#seconds').html(data[2]);
}
});
}
这篇关于如果我递归调用ajax函数会怎样?它可能会引起任何问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文