jQuery进度栏服务器端

use*_*764 2 ajax jquery struts2 jquery-ui struts2-jquery

我在服务器端(在db中运行查询数量)的过程运行时间长(不确定时间),耗时超过30秒。我想向用户显示进度百分比。我在应用程序中使用jquery,struts。有办法吗?

Bag*_*aga 5

这就是我们做到的方式。

  • 触发该过程后,请从客户端创建一个GUID,并将其传递给服务器。
  • 在服务器端,运行进程,并在运行期间,使用GUID作为键,将进度存储在会话/缓存中。
  • 在客户端,定期对通过GUID值的服务进行ajax调用。服务将返回与GUID值相对应的进度状态。
  • 基于从服务返回的值,更新ProgressBar状态。
  • 如果您将值存储在会话中,则在完成该过程后,请确保将其清除。

下面是进行ajax调用的示例方法。

    function updateProgress() {
        if (stopProgressCheck) return;
        var webMethod = progressServiceURL + "/GetProgress";
        var parameters = "{'guid':'" + guid + "'}"; //passing the guid value

        $.ajax({
            type: "POST",
            url: webMethod,
            data: parameters,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                if (msg.d != "NONE") { //add any necessary checks
                    //add code to update progress bar status using value in msg.d
                    statusTimerID = setTimeout(updateProgress, 100); //set time interval as required
                }
            },
            error: function (x, t, m) {
                alert(m);
            }
       });    
    }
Run Code Online (Sandbox Code Playgroud)

希望这对您有用:)