简体   繁体   English

jQuery移动加载时间

[英]jQuery mobile loading times

I'm using some jQuery on a web page that detects which menu option is current, when to display the mobile menu, etc. - I'm pretty much just experimenting with what is possible at the minute. 我在网页上使用一些jQuery,以检测哪个菜单选项是当前的,何时显示移动菜单等。-我几乎只是在试验可能的那一刻。 When I visit the page on a desktop PC, the code works fine, and my mobile menu and tabs load immediately when clicked. 当我在台式机上访问该页面时,该代码可以正常工作,并且单击该菜单后即可立即加载我的移动菜单和标签。

When I view the page on a mobile device (S5, Android) the loading times for the menu to show, the tabs to change, etc. take a few seconds, as opposed to immediately. 当我在移动设备(S5,Android)上查看页面时,显示菜单,更改选项卡等的加载时间要花费几秒钟,而不是立即。 I'm not too sure whether this is down to the efficiency of my code, or if I shouldn't be using particular functions or anything. 我不太确定这是否取决于代码的效率,还是我不应该使用特定的函数或任何东西。 Any ideas on what could be causing this delay? 关于什么可能导致此延迟的任何想法?

$(document).ready(function() {

    function getParam(name) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }

    var sortElement = currentSort(getParam('sort'));
    var itemElement = currentItem(getParam('items'));

    $(sortElement.element).addClass('current');
    $(itemElement.element).addClass('current');

    function currentSort(value) {

        var strElement;

        switch(value) {

            case 'newest' :
                strElement = '#newest';
            break;

            case 'oldest' :
                strElement = '#oldest';
            break;

            case 'alph_desc' :
                strElement = '#desc';
            break;

            case 'alph_asc' :
                strElement = '#asc';
            break;

            case '' :
                strElement = '#newest';
            break;

        } return {
            element: strElement
        }

    }

        function currentItem(value) {

        var strElement;

        switch(value) {

            case '10' :
                strElement = '#fewer';
            break;

            case '15' :
                strElement = '#few';
            break;

            case '50' :
                strElement = '#more';
            break;

            case '' :
                strElement = '#few';
            break;

        } return {
            element: strElement
        }

    }



    $('#search a').click(function() {
        $('#search').html('<form><input class="nav-search" placeholder="Search" type="text" name="search-query"/><input type="submit" value=" "/></form>');
    });

    function checkWidth() {
        var windowSize = $(window).width();

        if (windowSize < 1020) {
            $('.navigation .container').html('<ul><li class="brand mobile"><a href="/products"></a></li><li class="menu"></li><div class="mobile-menu"><ul><li><a href="/products"><div class="icon product"></div>Products</a></li><!----><li><a href="/personalise"><div class="icon personalise"></div>Personalise</a></li><!----><li id="search"><a><div class="icon search"></div>Search</a></li><!----><li><a href="/basket"><div class="icon basket"></div>Basket</a></li></ul></div></ul>');
            $('.mobile-menu').hide();
            $('.menu').click(function() {
                $('.mobile-menu').slideToggle();
            });
        } else {
            $('.navigation .container').html('<ul><li><a href="/products"><div class="icon product"></div>Products</a></li><!----><li><a href="/personalise"><div class="icon personalise"></div>Personalise</a></li><!----><li class="brand"><a href="/products"></a></li><!----><li id="search"><a><div class="icon search"></div>Search</a></li><!----><li><a href="/basket"><div class="icon basket"></div>Basket</a></li></ul>');
        }
    }

    checkWidth();
    $(window).resize(checkWidth);

});

I should comment your question first, but because my reputation is not enough, I will provide an answer then. 我应该先评论您的问题,但是由于我的声誉还不够,我将提供答案。 Have you tried to use switch case instead of if(s) for sortValue and itemValue ? 您是否尝试过使用switch case代替if(s)作为sortValueitemValue because AFAIK, the use of if statement will proceed to check each if case, as for switch case provided more efficient solution. 因为AFAIK,使用if语句将继续检查每个if情况,为switch case提供了更有效的解决方案。 check this question. 检查这个问题。

Just some pointers 只是一些指针

  • don't use docReady with JQM, use mobileInit (see here and jqm docs ) 不要将docReady与JQM一起使用,请使用mobileInit (请参阅此处jqm docs
  • mobile browsers are slower than "desktop", so "expensive" coding comes with a penalty. 移动浏览器比“桌面”浏览器慢,因此“昂贵”编码会带来损失。
  • everything you do can be done in plain JavaScript, so why use jQuery 您所做的一切都可以用普通的JavaScript完成,所以为什么要使用jQuery

No time to test and including many things I would never code like this, but this should give you enough pointers: 没有时间进行测试,包括很多我永远不会这样编写的东西,但这应该为您提供足够的指针:

(function (window, document) {
    var search_form, mobile_menu, desktop_menu;

    // > don't work with strings, build using documentElement;
    // > correct your strings, they are not valid!
    search_form = '<form><input class="nav-search" placeholder="Search" type="text" name="search-query"/><input type="submit" value=" "/></form>';
    mobile_menu = '<ul><li class="brand mobile"><a href="/products"></a></li><li class="menu"></li><div class="mobile-menu"><ul><li><a href="/products"><div class="icon product"></div>Products</a></li><!----><li><a href="/personalise"><div class="icon personalise"></div>Personalise</a></li><!----><li id="search"><a><div class="icon search"></div>Search</a></li><!----><li><a href="/basket"><div class="icon basket"></div>Basket</a></li></ul></div></ul>';
    desktop_menu = '<ul><li><a href="/products"><div class="icon product"></div>Products</a></li><!----><li><a href="/personalise"><div class="icon personalise"></div>Personalise</a></li><!----><li class="brand"><a href="/products"></a></li><!----><li id="search"><a><div class="icon search"></div>Search</a></li><!----><li><a href="/basket"><div class="icon basket"></div>Basket</a></li></ul>';

    // > JQM exposes methods to work with URL, look for $.mobile.path
    // > declare methods before they are needed
    // helper: get url parameter
    function getParam(name) {
      name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
      var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
          results = regex.exec(location.search);
      return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }

    // helper: get element showing sorting direction
    function currentSort(value) {
      var snip = value.split("_");
      return "#" + (snip[1] || snip[0]);
    }

    // helper: get element showing number of items
    function currentItem(value) {
      switch (value) {
        case '10': return "#fewer";
        case '50': return "#more";
        default: return "#few";
      }
    }

    // helper: set menu depending on real-estate available
    // ATTENTION: every time you call this you are setting a new binding!!!
    function checkWidth() {
      var window_size, $container;

      window_size = $(window).width();
      $container = $('.navigation .container');

      if (window_size < 1020) {
          $container.html(mobile_menu);
          $('.mobile-menu').hide();
          $('.menu').click(function() {
              $('.mobile-menu').slideToggle(desktop_menu);
          });
      } else {
          $container.html(desktop_menu);
      }
    }

    $(document).on("mobileinit", function () {
      var search;

      search = document.getElementById("search");

      // add classes to sorting and items selectors
      $( currentSort(getParam('sort')), currentItem(getParam('items')) ).addClass("current");

      // replace link with search form on click
      $( search ).on( "click", "a", function () {
        search.innerHTML = search_form;
      });

      // set desktop or mobile menu depending on screen width
      checkWidth();

    });

  }(window, document));

Hope that gets you on the right track! 希望能让您走上正确的道路!

From what I see what you are coding I don't think it explains a "few seconds" differential from desktop to mobile - unless fixing your html strings helps (passing jshint/jslint also helps imo...). 从我看到的代码来看,我认为它不能解释从台式机到移动设备的“几秒钟”差异-除非修复html strings帮助(传递jshint / jslint也可以帮助imo ...)。

After that you have to look elsewhere. 之后,您必须寻找其他地方。

In general, always keep in mind that mobile is slower than desktop and think twice about modifying the DOM, adding a lot of click bindings, etc, etc. I'm usually building the whole page pre-enhanced in memory and touch the DOM once (to inject it). 通常,请记住,移动设备比台式机慢,请三思而后行,以修改DOM,添加大量单击绑定等。我通常会在内存中预先构建整个页面,然后触摸DOM (注入)。 Helps a lot on slow devices. 对速度较慢的设备有很大帮助。

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

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