我有一个带7参数的工作ajax函数,但我今天主要关注的参数是第五个参数叫做“stop”。当第五个参数“stop”被设置为“true”时,当前的ajax函数会停止setTimeout,并且基本上不会再次调用自身,除非它再次从外部源触发。它有效,但问题在于:
而不是从ajax函数内部将stop变量设置为“true”,并将其传递给主ajax函数内的递归setTimeout ajax函数,为了阻止ajax函数再次调用自身,我需要检查我的数据库中的一些数据在 test6.php 中,如果是真的,那么我会调用函数timeoutAjax(),并为第五个参数传入“true”以阻止ajax函数调用自身试。
但出于某种原因我似乎无法做到这一点。在test6.php中调用函数timeoutAjax(),并为第五个参数传入“true”不起作用,即使在 test5.php <上传递第五个参数的“true”我会工作。不知道我做错了什么。请看一下我的代码:
test5.php
<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>";
?>
答案 0 :(得分:0)
看起来 test6.php 总是输出对timeoutAjax
的来电,对吗?当然,有时它会传递一个stop
参数,其值为true
,但在您对该变量执行任何操作以实际停止任何操作之前,您会启动另一个对 test6.php 的AJAX调用,正如我们所知,总是调用timeoutAjax
,这会让你进入一个循环。这是问题吗?
也许您只需要在document.getElementById(id).innerHTML = data;
支票内移动if (stop != true)
行,这样就不会通过 test6.php的回复重新调用timeoutAjax
如果您的stop
参数为true。但是如果你这样做了,那么在你传递stop
参数后,你仍然可以再次进行AJAX调用。
所以也许您只需要在AJAX通话之外移动stop
支票:
<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 输出脚本标记,如果不应该输出,则不输出脚本标记。