简体   繁体   English

在反应中显示加载百分比

[英]Show loading percentage in react

Hi I implemented a loader in react as shown below.嗨,我在反应中实现了一个加载器,如下所示。

 ReactDOM.render( "loaded", document.getElementById("root") );
 body, #root { width: 100%; margin: 0; display: flex; align-items: center; justify-content: center; background-color: #36364b; height: -webkit-fill-available; }
 <div id="root"> <img src="https://samherbert.net/svg-loaders/svg-loaders/ball-triangle.svg" class="loader" alt="loader"> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

This does show loading when it is loading.这确实在加载时显示加载。 It can be checked by disabling cache on chrome developer tools and reloading.可以通过禁用 chrome 开发人员工具上的缓存并重新加载来检查它。

I want to show the percent loaded for the script files.我想显示脚本文件的加载百分比。 How can this be implemented?如何实施?

The only method for getting the exact load percentage is use XHR (AJAX), but this is not possible for your case.获得确切负载百分比的唯一方法是使用 XHR (AJAX),但这对您的情况是不可能的。 The method is load scripts files with AJAX and then inyect the loaded code into the DOM.该方法是使用 AJAX 加载脚本文件,然后将加载的代码输入 DOM。 Using XHR you can use something like described in here: Can onprogress functionality be added to jQuery.ajax() by using xhrFields?使用 XHR,您可以使用此处描述的内容: Can onprogress 功能可以通过使用 xhrFields 添加到 jQuery.ajax()? However because the scripts are in a CDN, a different domain, you will have problems caused by the CORS / cross-site protections.但是,由于脚本位于不同域的 CDN 中,您将遇到由 CORS/跨站点保护引起的问题。 Or your CDN allows to load contents specifically from your domain (or any domain) or you cannot do in that way.或者您的 CDN 允许专门从您的域(或任何域)加载内容,或者您​​不能这样做。 See more inUnderstanding CORS了解 CORS 中查看更多信息

The other more inexact way is estimate the total percentage from the loaded files vs pending to load files (but not knowing which percentage of each file is loaded. You delay the script loading from the CDN, start your own "load management" script and then inyect in the dom the tags one by one (using something like described in Append <Script> Tag to Body? or in Loading scripts after page load? ), waiting to the window load (onload) events.另一种更不准确的方法是估计已加载文件与待加载文件的总百分比(但不知道每个文件的加载百分比。您延迟从 CDN 加载脚本,启动您自己的“加载管理”脚本,然后在 dom 中一一输入标签(使用类似于Append <Script> Tag to Body?或在页面加载后加载脚本中描述的内容? ),等待窗口加载 (onload) 事件。

This way you are going to know how is going the loading process, but roghly, so with that info you can draw a smoothed animation with a progress bar.通过这种方式,您将了解加载过程的进展情况,但很粗鲁,因此您可以使用该信息绘制带有进度条的平滑动画。

But beware: this will probably impact negatively on your load times because loading one by one the script files is usually slower than loading all at once (so the browser can manage the script loading process, usually paralellizing loadings).但要注意:这可能会对您的加载时间产生负面影响,因为一个接一个加载脚本文件通常比一次加载慢(因此浏览器可以管理脚本加载过程,通常是并行加载)。

Load bars are used usually on heavy dutty pages which load lots of resources which are usually on the same domain or at least domains that you own so you can setup CORS succesufully to let XHR (AJAX) loading processes from same or different domains.负载条通常用于加载大量资源的重载页面,这些资源通常位于同一域或至少是您拥有的域中,因此您可以成功设置 CORS 以让 XHR (AJAX) 加载过程来自相同或不同的域。 See more info here How does Access-Control-Allow-Origin header work?在此处查看更多信息Access-Control-Allow-Origin 标头如何工作? however this does not apply for your case as long as you cannot modify the CDN servers.但是,只要您无法修改 CDN 服务器,这不适用于您的情况。 If the servers allow CORS, you are lucky, if not, you probably cannot do anything.如果服务器允许 CORS,您很幸运,否则,您可能无法做任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM