Jquery progress bar server side

僤鯓⒐⒋嵵緔 提交于 2021-02-07 09:14:15

问题


I have a long running process(not definite time) in server-side(which runs number of queries in db) which takes more than 30s. I want to display the progress in % to the user. I'm using jquery ,struts in my application. Is there a way to do it?


回答1:


This is how we did it.

  • When the process is triggered create a GUID from the client and pass it to the server.
  • In the server side, run the process and during the run, keep storing the progress in session/cache using the GUID as key.
  • In the client side, make ajax calls periodically to a service passing the GUID value. The service will return the progress status corresponding to the GUID value.
  • Based on the value returned from the service update the ProgressBar status.
  • If you are storing the value in session, once the process is complete make sure you clear it.

Below is a sample method to make ajax calls.

    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);
            }
       });    
    }

Hope this is useful to you :)



来源:https://stackoverflow.com/questions/24608335/jquery-progress-bar-server-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!