带有ajaxStop的jQuery AJAX中的递归调用函数

时间:2022-11-29 13:49:13

I have a function:

我有一个功能:

function primaj(){
$('#tmp').load('msg1.php');
$("#tmp").ajaxStop(function(){
        //do some code
   primaj();
  });
}

What I expect is for the load request to run, and when it finishes the function to be called again. But I see that after every call to the function, the next number of calls doubles, from 1 to 2 to 4 to 8 and so on...

我期望的是加载请求运行,以及何时完成要再次调用的函数。但是我看到每次调用该函数后,下一个调用次数会翻倍,从1到2到4到8等等......

My intention is to make a continues load call to the server, but if I use a setInterval command (which works), sometimes the server takes longer to respond, thus the function is called over it's self.

我的目的是对服务器进行持续加载调用,但是如果我使用setInterval命令(有效),有时服务器需要更长的时间来响应,因此函数会通过它自己调用。

Is there a way to know when the ajax load command has stopped, and then call it again?

有没有办法知道ajax load命令何时停止,然后再次调用它?

1 个解决方案

#1


1  

It is simple at this:

这很简单:

function primaj() {
    $('#tmp').load('msg1.php', primaj);
}​

What you did, is attaching a new callback to run after every future ajax completion.
While what my code does is setting a callback to run after the current ajax completed.

你做了什么,在每个未来的ajax完成之后附加一个新的回调来运行。虽然我的代码所做的是设置回调以在当前ajax完成后运行。

#1


1  

It is simple at this:

这很简单:

function primaj() {
    $('#tmp').load('msg1.php', primaj);
}​

What you did, is attaching a new callback to run after every future ajax completion.
While what my code does is setting a callback to run after the current ajax completed.

你做了什么,在每个未来的ajax完成之后附加一个新的回调来运行。虽然我的代码所做的是设置回调以在当前ajax完成后运行。