douliang4858 2012-03-27 08:24
浏览 86
已采纳

用于启动javaScript Web应用程序的加载指示器

How can i make something similar like GMail has http://o7.no/H72gPf When application starting, gmail showing progress bar and it show us the percent (part) of loaded content already.

How i can mesure that certain JS file (HTML content, CSS or Images) loaded partially or fully ?

P.S. On server side I would like to use PHP

  • 写回答

2条回答 默认 最新

  • duanmo5724 2012-03-27 08:52
    关注

    You can do this in a number of ways.

    An easy way is to place your script tags (for included scripts) below the content, and have each included script call some function defined in the HEAD section of your document.

    So if you define this in a script tag in the head of your document:

    function increment_progress() {
       // your HTML-dependent implementation here
    }
    

    Then just do this at the bottom of included scripts:

    increment_progress();
    

    What increment_progress does really depends on what your progress-bar looks like, and how you build it with HTML.

    For example you could have a TABLE in a DIV, adding a TD to the first TABLE row each call to increment_progress, with fixed TD widths and different background colours for TD and DIV. You then choose a width for TD's which roughly equals (width of DIV) / (number of scripts to be loaded).

    Something like this:

    <html>
    <head>
      <style type="text/css">
        #your_interface { display: none; }
        #progressbar { width: 100px; background: #00ff00; }
        #progressbar table { width: 100%; margin: 0px; }
        #progressbar table td { padding: 0px; border: 0px; 
                                border-collapse: collapse; 
                                background: #ff0000; width: 10px; }
      </style>
      <script type="text/javascript" href="/path/to/jquery"></script>
      <script type="text/javascript">
        var count = 0;
        function increment_progress() {
           $('#progressbar table tr').eq(0).append('<td>&nbsp;</td>');
           count++;
           if (count == 9) { // the number of scripts loaded below
              $('#progressbar').hide();
              $('#your_interface').show();
           }
        }
      </script>
    <head>
    <body>
      <div id="progressbar"><table><tr><td></td></tr></table></div>
    
      <div id="your_interface">
         The rest of your content here!
      </div>
    
      <script type="text/javascript" href="/path/to/script1"></script>
      <script type="text/javascript" href="/path/to/script2"></script>
      <script type="text/javascript" href="/path/to/script3"></script>
      <script type="text/javascript" href="/path/to/script4"></script>
      <script type="text/javascript" href="/path/to/script5"></script>
      <script type="text/javascript" href="/path/to/script6"></script>
      <script type="text/javascript" href="/path/to/script7"></script>
      <script type="text/javascript" href="/path/to/script8"></script>
      <script type="text/javascript" href="/path/to/script9"></script>
    </body>
    </html>
    

    If you've got a fast server and fast internet connection, you'll barely see it :-)

    If you're already loading scripts using AJAX, you could call increment_progress() from your 'response' handler function.

    Update: to show how to transition to the real user interface after loading.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器