<bdo id='y33iy'></bdo><ul id='y33iy'></ul>

      1. <small id='y33iy'></small><noframes id='y33iy'>

      2. <tfoot id='y33iy'></tfoot>

        <legend id='y33iy'><style id='y33iy'><dir id='y33iy'><q id='y33iy'></q></dir></style></legend>
        <i id='y33iy'><tr id='y33iy'><dt id='y33iy'><q id='y33iy'><span id='y33iy'><b id='y33iy'><form id='y33iy'><ins id='y33iy'></ins><ul id='y33iy'></ul><sub id='y33iy'></sub></form><legend id='y33iy'></legend><bdo id='y33iy'><pre id='y33iy'><center id='y33iy'></center></pre></bdo></b><th id='y33iy'></th></span></q></dt></tr></i><div id='y33iy'><tfoot id='y33iy'></tfoot><dl id='y33iy'><fieldset id='y33iy'></fieldset></dl></div>

        具有完成阶段的多个 ajax 请求的进度条.是否可以

        时间:2023-10-13
        <tfoot id='kWmyf'></tfoot>
          <tbody id='kWmyf'></tbody>

            <i id='kWmyf'><tr id='kWmyf'><dt id='kWmyf'><q id='kWmyf'><span id='kWmyf'><b id='kWmyf'><form id='kWmyf'><ins id='kWmyf'></ins><ul id='kWmyf'></ul><sub id='kWmyf'></sub></form><legend id='kWmyf'></legend><bdo id='kWmyf'><pre id='kWmyf'><center id='kWmyf'></center></pre></bdo></b><th id='kWmyf'></th></span></q></dt></tr></i><div id='kWmyf'><tfoot id='kWmyf'></tfoot><dl id='kWmyf'><fieldset id='kWmyf'></fieldset></dl></div>

              <small id='kWmyf'></small><noframes id='kWmyf'>

                <legend id='kWmyf'><style id='kWmyf'><dir id='kWmyf'><q id='kWmyf'></q></dir></style></legend>
                • <bdo id='kWmyf'></bdo><ul id='kWmyf'></ul>

                • 本文介绍了具有完成阶段的多个 ajax 请求的进度条.是否可以?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个简单的表单,提交时通过 ajax 请求进行验证.如果表单检查正常,则发出另一个 ajax 请求来处理最初提交的数据.

                  I have a simple form which when submitted validates via an ajax request. If the form checks out ok, then another ajax request is made to process the data originally submitted.

                  我想为此建立一个进度条.我发现将此代码添加到每个 ajax 请求会分别返回每个调用的进度.这使得进度条加载到 100%,两次,很快.

                  I want to build a progress bar for this. Ive found that adding this code to each ajax request returns the progress for each call separately. That makes the progress bar load to 100%, twice, quickly.

                  是否有可能例如两个 ajax 请求每个填充进度条的 50%?...所以 ajax 请求 1 将填充到 50%,第二个将从 51% 填充到 100%?还是疯了?

                  Is it possible for example for two ajax request to each fill 50% of the progress bar?... So ajax request 1 will fill up to 50% and the second will fill from 51% to 100%? Or is that crazy?

                  或者如果三个 ajax 调用各占总百分比的 33.33%?

                  Or if three ajax calls each being responsible for 33.33% of the total percentage?

                  我想我们更多地关注完成的阶段以及进度.

                  I guess we are more looking at stages of completion as well as progress.

                  有什么想法可以在不过多伪装的情况下实现这一点吗?

                  Any ideas how this could be achieved without too much faking it?

                  var xhr = new window.XMLHttpRequest();
                  //Upload progress
                  xhr.upload.addEventListener("progress", function (evt) {
                      if (evt.lengthComputable) {
                          var percentComplete = evt.loaded / evt.total;
                          //Do something with upload progress
                          console.log('percent uploaded: ' + (percentComplete * 100));
                      }
                  }, false);
                  //Download progress
                  xhr.addEventListener("progress", function (evt) {
                      if (evt.lengthComputable) {
                          var percentComplete = evt.loaded / evt.total;
                          //Do something with download progress
                          console.log('percent downloaded: ' + (percentComplete * 100));
                      }
                  }, false);
                  return xhr;
                  

                  推荐答案

                  是的,有可能.您可以创建一个包含每个 ajax 调用的数组.将 元素 max 属性设置为 100/array.length.将单个 progress 事件的 evt.loaded/evt.total 除以数组 .length 以设置 value><progress> 元素.您还可以使用 Promise.all().then() 来处理从 ajax 调用返回 Promise 的函数数组并更新 <progress> 元素.

                  Yes, it is possible. You can create an array containing each ajax call. Set <progress> element max attribute to 100/array.length. Divide evt.loaded / evt.total of individual progress event by array .length to set value of <progress> element. You could also use Promise.all(), .then() to process array of functions returning a Promise from ajax call and update <progress> element.

                  html

                  <label></label>
                  <progress value="0" min="0" max="100"></progress>
                  

                  javascript

                  var progress = document.querySelector("progress");
                  var url = "/echo/html/";
                  
                  function request(url) {
                  var len = arr.length;
                  return new Promise(function(resolve, reject) {
                    var xhr = new XMLHttpRequest();
                    xhr.upload.addEventListener("progress", function(evt) {
                      if (evt.lengthComputable) {   
                        var percentComplete = ((evt.loaded / evt.total) * (progress.max / len)) 
                                              / len;
                        progress.value += percentComplete;
                        console.log(progress.value, percentComplete);
                        if (evt.total === evt.loaded) {
                          requests += 1;
                        }
                        if (progress.value == progress.max && requests === len) {
                          progress.previousElementSibling.innerHTML = "upload complete";
                          // you could call `resolve()` here if only interested in
                          // `upload` portion of request
                          alert("upload complete");
                        }
                      }
                    }, false);
                    xhr.onload = function() {
                       resolve(this.responseText)
                    };
                    xhr.onerror = reject;
                    xhr.open("POST", url, true)
                    xhr.send("html=" + Array(100000).fill(1).join(""));
                    })
                  }
                  
                  var arr = [], requests = 0;
                  arr.push(request, request, request);
                  Promise.all(arr.map(function(req) {
                    return req(url)
                  }))
                  .then(function(data) {
                    console.log(data);    
                  })
                  .catch(function(err) {
                    console.log(err)
                  })
                  

                  jsfiddle https://jsfiddle.net/v2msL7hj/3/

                  这篇关于具有完成阶段的多个 ajax 请求的进度条.是否可以?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:XMLHttpRequest 上传进度未正确触发 下一篇:Google Chrome:当凭证标志为真时,不能在“Access-C

                  相关文章

                  最新文章

                    <small id='j5PTn'></small><noframes id='j5PTn'>

                      <bdo id='j5PTn'></bdo><ul id='j5PTn'></ul>
                  1. <legend id='j5PTn'><style id='j5PTn'><dir id='j5PTn'><q id='j5PTn'></q></dir></style></legend>
                    1. <tfoot id='j5PTn'></tfoot>
                      <i id='j5PTn'><tr id='j5PTn'><dt id='j5PTn'><q id='j5PTn'><span id='j5PTn'><b id='j5PTn'><form id='j5PTn'><ins id='j5PTn'></ins><ul id='j5PTn'></ul><sub id='j5PTn'></sub></form><legend id='j5PTn'></legend><bdo id='j5PTn'><pre id='j5PTn'><center id='j5PTn'></center></pre></bdo></b><th id='j5PTn'></th></span></q></dt></tr></i><div id='j5PTn'><tfoot id='j5PTn'></tfoot><dl id='j5PTn'><fieldset id='j5PTn'></fieldset></dl></div>