基于ajax响应循环jquery ajax..使用setTimeout
因此,我尝试使用单个 jquery 函数,该函数使用 ajax 与单个 php 页面对话,但调用流程的不同步骤。如果流程步骤成功完成,它将返回下一步,以便我可以再次循环该函数......直到我返回“完成”或“失败”。我想安排该过程的某些步骤的时间,以解决延迟的服务器端处理问题。我在函数内部使用 setTimeout 时遇到问题。我有以下问题:
function ProcessClient(InProcess){
var dataString = 'inprocess='+InProcess;
if(InProcess=='buildaccount'){dataString+='&vercode='+$('#vercode').val();}
//append current step(status) to client here
$.ajax({
type: "POST",
url: 'ajax/NewClient.php',
data: dataString,
success: function(data){
if(data=='Fail'){
alert(data);
}else if(data=='Complete'){
$('#Progress').append('<li>All done! Click <a href="/manage/">here</a> to head over to your administration page!</li>');
}else{
var Timer = 1000;
if(data=='buildserveraccounts'){
Timer=10000;
}
$(function(){
setTimeout('ProcessClient('+data+')',Timer);
})
}
}
});
}
我在这里做错了什么?截至目前......该功能甚至没有循环......我已经尝试了一些不同的事情,但永远无法让计时器工作。哦,我愿意接受关于更好的方法来完成我正在尝试的事情的想法......?
So I'm trying to use a single jquery function that uses ajax to talk to a single php page but calls different steps of a process. If the process step completes successfully, it returns the next step so that i can loop through the function again... until I return a 'complete' or 'fail'. I want to time certain steps of the process to account for delayed serverside processing. I'm having trouble using setTimeout inside of the function. I have the following:
function ProcessClient(InProcess){
var dataString = 'inprocess='+InProcess;
if(InProcess=='buildaccount'){dataString+='&vercode='+$('#vercode').val();}
//append current step(status) to client here
$.ajax({
type: "POST",
url: 'ajax/NewClient.php',
data: dataString,
success: function(data){
if(data=='Fail'){
alert(data);
}else if(data=='Complete'){
$('#Progress').append('<li>All done! Click <a href="/manage/">here</a> to head over to your administration page!</li>');
}else{
var Timer = 1000;
if(data=='buildserveraccounts'){
Timer=10000;
}
$(function(){
setTimeout('ProcessClient('+data+')',Timer);
})
}
}
});
}
What am I doing wrong here? As of now... the function isn't even looping.. I've tried a few different things and can never get the timer to work. Oh and I'm open to ideas on better ways to accomplish what I'm trying to... ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码在几个方面是可疑的:
首先,您将一个函数传递到
$()
中,它是$(docmument).ready(...)
——例如,“当 DOM 准备好时给我打电话”钩子。这段代码与 DOM 就绪无关。这在这里相对无害,因为如果 DOM 已经准备好,jQuery 会立即调用该函数,但这充其量是不必要的。第二件事是您将一个字符串传递到
setTimeout
中。几乎没有充分的理由这样做,它与使用 eval 相同(但有延迟),也几乎没有理由使用。相反,传入函数引用。所以:
我预计它根本不起作用的原因是您传递给
setTimeout
的字符串是ProcessClient(SomeDataTest)
而不是ProcessClient("SomeDataTest ")
(注意引号),因此当稍后评估该字符串时,这是一个语法错误并且未执行。This code is suspect in a couple of ways:
The first thing is you're passing a function into
$()
, which is shorthand for$(docmument).ready(...)
— e.g., the "call me when the DOM is ready" hook. This code has nothing to do with DOM ready. That would be relatively harmless here because if the DOM is already ready, jQuery calls the function immediately, but it's unnecessary at best.The second thing is that you're passing a string into
setTimeout
. There's almost never a good reason to do that, it's the same as usingeval
(but with the delay), which there's also almost never a reason to use. Instead, pass in a function reference.So:
I expect the reason it wasn't working at all was that the string you were passing into
setTimeout
wasProcessClient(SomeDataTest)
rather thanProcessClient("SomeDataTest")
(note the quotes), and so when the string was evaluated later, it was a syntax error and not executed.