jQuery Ajax / .each 回调,下一个“each”在ajax完成之前触发
您好,当我提交表单时,会调用下面的 Javascript。它首先从文本区域中分割出一堆 url,然后:
1) 为每个 url 添加行到表中,并在最后一列(“状态”列)中显示“未开始”。
2) 它再次循环遍历每个 url,首先它会进行 ajax 调用来检查状态 (status.php),这将返回 0 - 100 之间的百分比。
3)在同一个循环中,它通过ajax(process.php)启动实际流程,当流程完成时(记住持续的状态更新),它会在状态栏中显示“已完成”并退出自动刷新。
4) 然后它应该转到下一个“each”并对下一个 url 执行相同的操作。
function formSubmit(){
var lines = $('#urls').val().split('\n');
$.each(lines, function(key, value) {
$('#dlTable tr:last').after('<tr><td>'+value+'</td><td>Not Started</td></tr>');
});
$.each(lines, function(key, value) {
var auto_refresh = setInterval( function () {
$.ajax({
url: 'status.php',
success: function(data) {
$('#dlTable').find("tr").eq(key+1).children().last().replaceWith("<td>"+data+"</td>");
}
});
}, 1000);
$.ajax({
url: 'process.php?id='+value,
success: function(msg) {
clearInterval(auto_refresh);
$('#dlTable').find("tr").eq(key+1).children().last().replaceWith("<td>completed rip</td>");
}
});
});
}
Hi the below Javascript is called when I submit a form. It first splits a bunch of url's from a text area, it then:
1) Adds lines to a table for each url, and in the last column (the 'status' column) it says "Not Started".
2) Again it loops through each url, first off it makes an ajax call to check on the status (status.php) which will return a percentage from 0 - 100.
3) In the same loop it kicks off the actual process via ajax (process.php), when the process has completed (bearing in the mind the continuous status updates), it will then say "Completed" in the status column and exit the auto_refresh.
4) It should then go to the next 'each' and do the same for the next url.
function formSubmit(){
var lines = $('#urls').val().split('\n');
$.each(lines, function(key, value) {
$('#dlTable tr:last').after('<tr><td>'+value+'</td><td>Not Started</td></tr>');
});
$.each(lines, function(key, value) {
var auto_refresh = setInterval( function () {
$.ajax({
url: 'status.php',
success: function(data) {
$('#dlTable').find("tr").eq(key+1).children().last().replaceWith("<td>"+data+"</td>");
}
});
}, 1000);
$.ajax({
url: 'process.php?id='+value,
success: function(msg) {
clearInterval(auto_refresh);
$('#dlTable').find("tr").eq(key+1).children().last().replaceWith("<td>completed rip</td>");
}
});
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您想要的是按顺序运行多个异步操作,对吗?我将构建一个函数数组来执行并通过序列助手运行它。
https://github.com/michiel/asynchelper-js/blob /master/lib/sequencer.js
如您所见,我们构建了一个作用域函数数组,这些函数接受任意回调作为参数。定序器将为您按顺序运行它们。
使用 github 链接中的序列助手并运行,
顺便说一句,可以在几行代码中定义序列器函数。例如,
What you want is to run several asynchronous actions in sequence, right? I'd build an array of the functions to execute and run it through a sequence helper.
https://github.com/michiel/asynchelper-js/blob/master/lib/sequencer.js
As you can see, we build an array of scoped functions that take an arbitrary callback as an argument. A sequencer will run them in order for you.
Use the sequence helper from the github link and run,
It is, btw, possible to define sequencer functions in a few lines of code. For example,
AJAX 是异步。
这正是应该发生的事情。
您应该在前一个 AJAX 请求的完成处理程序中发送下一个 AJAX 请求,而不是使用
each
。AJAX is asynchronous.
That's exactly what's supposed to happen.
Instead of using
each
, you should send the next AJAX request in the completion handler of the previous one.您还可以使用“async”属性将 AJAX 设置为同步运行。添加以下内容:
AJAX API 参考此处。请注意,这将锁定浏览器,直到请求完成。
我更喜欢 SLAks 答案中的方法(坚持异步行为)。但是,这确实取决于您的应用程序。谨慎行事。
You can also set AJAX to run synchronously using the "async" property. Add the following:
AJAX API reference here. Note that this will lock the browser until the request completes.
I prefer the approach in SLaks answer (sticking with asynchronous behavior). However, it does depend on your application. Exercise caution.
我会给出与此相同的答案 jquery json populate table
此代码将给出您知道如何将回调与循环和 ajax 一起使用。但我没有测试过,肯定会有bug。我从旧代码中得出以下结论:-
I would give the same answer as this jquery json populate table
This code will give you a little idea how to use callback with loops and ajax. But I have not tested it and there will be bugs. I derived the following from my old code:-