有没有办法判断最后一个ajax调用是否完成?

发布于 2024-12-16 20:11:00 字数 755 浏览 0 评论 0原文

我正在处理这个网站,我需要弄清楚最后一次是什么时候ajax 调用完成...我正在使用这个 jQuery 插件,一切都很好,但是问题是客户端一次上传 2 个文件...并且我需要在最后一次 ajax 调用后重定向到另一个页面。如果您在上传文件时在 firebug 中查看它,它会运行 http://dev.posnation.com/test/j/example/upload.php 两次,并且我需要在且仅此后进行页面重定向第二次运行后..javascript 或 jQuery 是否有方法来判断 ajax 调用是否完成。

这是实例化它的代码

// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload();

,并且 jQuery.fileupload.js 中有一个 onDone 函数,

    _onDone: function (result, textStatus, jqXHR, options) {

但它在第一次运行后运行,而不是在两个文件上传后运行....有关如何在两个文件上传后重定向到另一个页面的任何想法已上传....提前致谢

I have this site that i am working on that i need to figure out when the last ajax call is finished...I am using this jQuery plugin and all works great but the problem is the client uploads 2 files at a time... and i need to redirect to another page after the last ajax call. If you look the it in firebug while uploading files it run http://dev.posnation.com/test/j/example/upload.php twice and i need to have the page redirect after and only after the second run..is there a way in javascript or jQuery to tell if the ajax calls are complete.

Here is the code that instantiates it

// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload();

and there is a onDone function in the jQuery.fileupload.js

    _onDone: function (result, textStatus, jqXHR, options) {

but its running after the first run not after both files are uploaded....any ideas on how i can redirect to another page after both files are uploaded....thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一梦浮鱼 2024-12-23 20:11:00

简短的答案是减少 onDone 回调中的计数器。一种可能的实现是使用全局变量,如下所示:

var numberFilesToUpload = 2;

// code to upload file
onDone: function() {
 // this is the upload plug-ins callback function
 numberFilesToUpload--;
 if (numberFileToUpload == 0) {
   windows.location.href = "/uploading-finished";
 }
}

还有更优雅的解决方案,但它们都涉及递减计数器。

the short answer is to decrement a counter in the onDone callback. one possible implementation would be to use a global variable as illustrated below:

var numberFilesToUpload = 2;

// code to upload file
onDone: function() {
 // this is the upload plug-ins callback function
 numberFilesToUpload--;
 if (numberFileToUpload == 0) {
   windows.location.href = "/uploading-finished";
 }
}

there are more elegant solutions, but they will all involve decrementing a counter.

ゃ人海孤独症 2024-12-23 20:11:00

您可以使用 closure 来生成具有已上传文件的本地记录的函数。

var fileUploads = function (filesNum, callback) {
    var numberFilesToUpload = filesNum;
    // code to upload file
    return function() {
     // this is the upload plug-ins callback function
     numberFilesToUpload--;
     if (numberFileToUpload == 0) {
       callback();
     }

    };

}(2, function () {
  //what you want to do when files are all uploaded.
});

fileUploads 将在上传 2 个文件后触发回调函数。回调和文件限制在此行中指定

var fileUploads = function () {
  //...
} (2, function () {});

You could use closure to produce the function which has a local records for files have uploaded.

var fileUploads = function (filesNum, callback) {
    var numberFilesToUpload = filesNum;
    // code to upload file
    return function() {
     // this is the upload plug-ins callback function
     numberFilesToUpload--;
     if (numberFileToUpload == 0) {
       callback();
     }

    };

}(2, function () {
  //what you want to do when files are all uploaded.
});

fileUploads will fire the callback function after 2 files are uploaded. The callback and filelimits are specified in this line

var fileUploads = function () {
  //...
} (2, function () {});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文