分析 jQuery,如何让我的应用程序更加敏捷?

发布于 2024-08-02 23:59:42 字数 3778 浏览 4 评论 0原文

我对 jQuery 函数的调用次数达到了 14,000 次……到底是怎么回事?我没有那么多函数,实际上只是简单的东西,比如附加和删除 DOM 元素,为什么我的一些事件处理程序调用函数这么多次?

另外,让我的问题更加复杂的是,Firebug 的探查器只显示了 min'd 函数名称...即使我使用未压缩的库,它也大多只显示 init()$.()< /code>

有人有什么技巧吗?

所以我知道这很多,但看起来效率很低,它在我们的页面 newgoldleaf.com 上执行,有些功能需要近 50 毫秒才能运行......是很长的时间还是只是我?

// prepare ajax for form posts
jQuery.ajaxSetup({
  "beforeSend" : function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

// initializes panels and gets url hash, shows correct panel
jQuery.fn.initPanes = function() {
  $("div#main_content > div:not(.message)").addClass("inactive");

  var hash = window.location.hash.substr(1);
  this.each(function () {

    if ($(this).hasClass(hash)) {
      var panelToShow = "." + $(this).attr("class");
      $(panelToShow).removeClass("inactive").toggleClass("active");
    }
  });

  // if no hash found in url, activate first menu item   
  if (hash == "" ) {
    $(this).eq(0).activatePane(); 
  }

};

// shows panel when user clicks sidebar links
jQuery.fn.activatePane = function(pane) {

  if ($(this).hasClass("unavailable") == true) {
    return false;
  }

  if ($(this).hasClass("active") == false) {
    $("div#main_content > div:not(.message)").hide().removeClass("active").addClass("inactive");
    $(this).siblings().removeClass("active");
    var panelToShow = "div#main_content div." + $(this).attr("class");

    // set the hash in the url
    window.location.hash = $(this).attr("class");

    $(this).toggleClass("active");
    $(panelToShow).fadeIn("slow").removeClass("inactive").addClass("active");
  };
};

jQuery.fn.functionName = function() {

};

$(document).ready(function (){

  $('ul.examples li:not(img, h5, a)').hover(function (){
    var bubble = $(this).find("h5.bubble")
    bubble.animate({
      opacity:".99",
      bottom:"28px"
    }, 200);
  }, function (){
    var bubble = $(this).find("h5.bubble")
    bubble.animate({
      opacity:"0",
      bottom:"38px"
    }, 200).animate({
      bottom:"20px"
    }, 0);
  });


  // hide/show comment form for users with javascript
  $("div#comments_not_allowed").hide();
  $("form#new_comment").show();

  // $("body#index div.preview").slideShow();


  // error and flash notice animation
  $(".message").animate({
    opacity: "1",
  }, 2000).animate({
    opacity: "0",
  }, 2000).hide(500);

  // home page caption bubble for blog image fade in
  $("body#index h5.bubble").fadeIn("slow");
  $("body#index h5.bubble").animate({
    bottom: "22px",
    opacity: ".99"
  }, 1000);

  $("form#new_comment").submit(function() {
      $.post($(this).attr("action"), $(this).serialize(), null, "script");

    return false;
  });

  $("form#new_lead").submit(function() {
      $.post($(this).attr("action"), $(this).serialize(), null, "script");
    return false;
  });



  if ($("ul.panels").length > 0 ) {
    // panel animation
    $("div#aside ul li").initPanes();
    $("div#aside ul li").css({
      cursor:"pointer"
    });


    $("div#aside ul li").click(function () {
      $(this).activatePane();  
    });
  };

  $(document).load(function() {
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

    function startAnalytics() {
        var pageTracker = _gat._getTracker("UA-7434953-1");
        pageTracker._initData();
        pageTracker._trackPageview();
    }

    if (window.addEventListener) {
        window.addEventListener('load', startAnalytics, false);
    }
    else if (window.attachEvent) {
        window.attachEvent('onload', startAnalytics);
    }
  })


})

I have some calls to jQuery functions like 14,000 times... what the hell? I don't have that many functions, really just simple stuff like appending and removing DOM elements, why do some of my event handlers call functions so many times?

Plus to compound my issues, Firebug's profiler just show's the min'd functions names... and even when I use the uncompressed library it mostly just shows init() or $.()

Does anyone have any tricks?

So I know this is a lot, but it seems really inefficient, it executes on our page newgoldleaf.com, some of the functions take almost 50ms to run... is that a long time or is it just me?

// prepare ajax for form posts
jQuery.ajaxSetup({
  "beforeSend" : function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

// initializes panels and gets url hash, shows correct panel
jQuery.fn.initPanes = function() {
  $("div#main_content > div:not(.message)").addClass("inactive");

  var hash = window.location.hash.substr(1);
  this.each(function () {

    if ($(this).hasClass(hash)) {
      var panelToShow = "." + $(this).attr("class");
      $(panelToShow).removeClass("inactive").toggleClass("active");
    }
  });

  // if no hash found in url, activate first menu item   
  if (hash == "" ) {
    $(this).eq(0).activatePane(); 
  }

};

// shows panel when user clicks sidebar links
jQuery.fn.activatePane = function(pane) {

  if ($(this).hasClass("unavailable") == true) {
    return false;
  }

  if ($(this).hasClass("active") == false) {
    $("div#main_content > div:not(.message)").hide().removeClass("active").addClass("inactive");
    $(this).siblings().removeClass("active");
    var panelToShow = "div#main_content div." + $(this).attr("class");

    // set the hash in the url
    window.location.hash = $(this).attr("class");

    $(this).toggleClass("active");
    $(panelToShow).fadeIn("slow").removeClass("inactive").addClass("active");
  };
};

jQuery.fn.functionName = function() {

};

$(document).ready(function (){

  $('ul.examples li:not(img, h5, a)').hover(function (){
    var bubble = $(this).find("h5.bubble")
    bubble.animate({
      opacity:".99",
      bottom:"28px"
    }, 200);
  }, function (){
    var bubble = $(this).find("h5.bubble")
    bubble.animate({
      opacity:"0",
      bottom:"38px"
    }, 200).animate({
      bottom:"20px"
    }, 0);
  });


  // hide/show comment form for users with javascript
  $("div#comments_not_allowed").hide();
  $("form#new_comment").show();

  // $("body#index div.preview").slideShow();


  // error and flash notice animation
  $(".message").animate({
    opacity: "1",
  }, 2000).animate({
    opacity: "0",
  }, 2000).hide(500);

  // home page caption bubble for blog image fade in
  $("body#index h5.bubble").fadeIn("slow");
  $("body#index h5.bubble").animate({
    bottom: "22px",
    opacity: ".99"
  }, 1000);

  $("form#new_comment").submit(function() {
      $.post($(this).attr("action"), $(this).serialize(), null, "script");

    return false;
  });

  $("form#new_lead").submit(function() {
      $.post($(this).attr("action"), $(this).serialize(), null, "script");
    return false;
  });



  if ($("ul.panels").length > 0 ) {
    // panel animation
    $("div#aside ul li").initPanes();
    $("div#aside ul li").css({
      cursor:"pointer"
    });


    $("div#aside ul li").click(function () {
      $(this).activatePane();  
    });
  };

  $(document).load(function() {
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

    function startAnalytics() {
        var pageTracker = _gat._getTracker("UA-7434953-1");
        pageTracker._initData();
        pageTracker._trackPageview();
    }

    if (window.addEventListener) {
        window.addEventListener('load', startAnalytics, false);
    }
    else if (window.attachEvent) {
        window.attachEvent('onload', startAnalytics);
    }
  })


})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文