我有一个有7个参数的ajax函数,但我今天主要关注的参数是第五个参数"stop"。当第五个参数"stop"设置为"true"时,当前ajax函数停止setTimeout,并且基本上不再调用自己,除非它再次从外部源触发。它可以工作,但这里有一个问题:
而不是将停止变量设置为"true"从ajax函数内部,并将其传递给主ajax函数内部的递归setTimeout ajax函数,为了阻止ajax函数再次调用自己,我需要在test6.php中检查数据库中的一些数据,如果它是真的,那么我将调用函数timeoutAjax(),并传入"true"为第五个参数阻止ajax函数再次调用自己。
但是由于某种原因,我似乎做不到。在test6.php中调用函数timeoutAjax(),并为第五个参数传入"true"是行不通的,即使在test5.php 上为第五个参数传入"true"将工作。不知道我做错了什么。请看看我的代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
var queues = {};
function timeoutAjax(url, theName, id, timeout, stop, addBy, rewindAt) {
var input = document.getElementById(theName).value; // or $('#t').val();
if (stop == "true") {
stop = true;
}
clearInterval(queues[theName]);
// make the call
$.ajax({
type: "POST",
url: url,
data: { input : input, timeout : timeout },
error: function (xhr, status, error) {
alert(error);
},
success: function (data) {
document.getElementById(id).innerHTML = data;
// queue a new call
if (stop != true) {
queues[theName] = setInterval(function () {
timeout+=addBy;
if (timeout >= (rewindAt+addBy)) {
timeout = addBy;
}
timeoutAjax(url, theName, id, timeout, stop, addBy, rewindAt);
}, timeout);
}
},
complete: function(){
$('input[name="'+theName+'"]').prop("disabled",false);
},
beforeSend: function(){
$('input[name="'+theName+'"]').prop("disabled",true);
}
});
}
</script>
<textarea id = 't' rows = "25" cols = "50" onKeyDown="if(event.keyCode==13) timeoutAjax('test6.php','t','output',3000,'false',3000,15000)"> </textarea>
<div id = 'output'> </div>
test6.php
<?php
$input = nl2br($_POST['input']);
echo $input
//checks something in database.
//if true, set the fifth parameter in the ajax function to true
echo "<script>
timeoutAjax('test6.php','t','output',3000,'true',3000,15000);
</script>";
?>
看起来test6.php总是输出对timeoutAjax
的调用,对吗?当然,有时它传递一个stop
参数,计算为true
,但在您使用该变量执行任何操作以实际停止任何操作之前,您启动对test6.php的另一个AJAX调用,如我们所知,总是调用timeoutAjax
,这将使您进入循环。这是问题所在吗?
也许您只需要将document.getElementById(id).innerHTML = data;
行移动到if (stop != true)
检查中,这样如果您的stop
参数为真,您就不会用test6.php的响应重新调用timeoutAjax
。但是如果您这样做了,在传递stop
参数之后,您仍然会再次执行AJAX调用。
所以也许你只需要移动你的stop
检查之外的AJAX调用:
<script type = "text/javascript">
var queues = {};
function timeoutAjax(url, theName, id, timeout, stop, addBy, rewindAt) {
var input = document.getElementById(theName).value; // or $('#t').val();
if (stop == "true") {
return; // just return here so you don't make the ajax call again.
}
clearInterval(queues[theName]);
// make the call
$.ajax({
type: "POST",
url: url,
data: { input : input, timeout : timeout },
error: function (xhr, status, error) {
alert(error);
},
success: function (data) {
document.getElementById(id).innerHTML = data;
// queue a new call
queues[theName] = setInterval(function () {
timeout+=addBy;
if (timeout >= (rewindAt+addBy)) {
timeout = addBy;
}
timeoutAjax(url, theName, id, timeout, stop, addBy, rewindAt);
}, timeout);
},
complete: function(){
$('input[name="'+theName+'"]').prop("disabled",false);
},
beforeSend: function(){
$('input[name="'+theName+'"]').prop("disabled",true);
}
});
}
</script>
实际上,看起来您还设置了一个间隔,以便定期调用timeoutAjax。因此,您不仅通过test6.php输出调用它,而且还以间隔调用它。这是你的本意吗?也许你根本不需要setInterval
。甚至可能不是stop
参数。如果需要再次调用,只需让test6.php像现在一样输出脚本标签,如果不需要,则不输出脚本标签。