简体   繁体   English

如何使此javascript更有效率?

[英]How can I make this javascript more efficient?

i am receiving a piece of plain html from an ajax request. 我从ajax请求中收到一段纯HTML。

<h1>Title</h1>
<div>
  content
</div>

This is the most simple form. 这是最简单的形式。 Every piece contains a <h1> tag for a title, and a <div> tag containing the content. 每一块都包含一个标题的<h1>标签和一个包含内容的<div>标签。 I have a nicely formatted container in the html page which needs to populate with the returned html snippet. 我在html页面中有一个格式良好的container ,需要使用返回的html代码段进行填充。

This is the container: 这是容器:

<div id="container">
  <div class="header">
  </div>
  <div class="content">
  </div>
</div>

I use the following javascript function to parse the html and place it in the container . 我使用以下javascript函数解析html并将其放置在container

function loadContent(id, data) {
    var container = $('#'+id);
    var title = '';
    var content = '';

    $(data).filter('h1:first').each(function() {
        title = $(this).html();
        content = $(this).next().html();
    });

    $('div.header',container).html(title);
    $('div.content', container).html(content);
}

Everything seems to be working 'allright', subsequent ajax requests that have different html contents load pretty quickly. 一切似乎都“正常”,随后具有不同html内容的ajax请求将很快加载。 But when I click a link that invokes a full page refresh, it hangs for about 3 or 4 seconds before loading clicked hyperlink. 但是,当我单击调用整个页面刷新的链接时,它会挂起大约3或4秒钟,然后再加载单击的超链接。 This makes me think it is a javascript issue, maybe where some content stays in memory? 这使我认为这是一个JavaScript问题,也许某些内容保留在内存中? Can somebody see where this might become inefficient? 有人可以看到这可能变得效率低下吗?

I'm not sure if I understand the intent here. 我不确定我是否了解这里的意图。 If you're only assigning one title/content pair, why the .each()? 如果只分配一个标题/内容对,为什么要使用.each()?

Also, keep in mind that Firebug often causes significant performance issues itself. 另外,请记住,Firebug本身通常会引起严重的性能问题。 Be sure to test with it disabled before assuming your code is the problem. 在假设您的代码是问题之前,请务必先禁用它进行测试。

Finally, if that's not it, use Firebug's profiler to determine exactly which part of your code is running slowly. 最后,如果不是,请使用Firebug的探查器准确确定代码的哪一部分运行缓慢。

Update , based on comment. 根据评论更新 Try this instead: 尝试以下方法:

function loadContent(id, data) {
  var container = document.getElementById(id);

  var title = '';
  var content = '';

  $data = $(data);
  $title = $data.filter('h1:first');

  title = $title.html();
  content = $title.next().html();

  $('div.header', container).html(title);
  $('div.content', container).html(content);
}

I would use a debugger to find the source of the delay. 我将使用调试器查找延迟的来源。 Something like FireFox w/FireBug to verify that the link that triggers the full page refresh is actually calling this code and not getting hung up elsewhere. 带有FireBug的FireFox之类的东西可以验证触发整个页面刷新的链接实际上是在调用此代码,而不会挂在其他地方。 And FireFox w/TamperData to watch the requests that are going out and coming back to see if the delay is caused by waiting on an external response. FireFox w / TamperData可以监视正在发出并返回的请求,以查看延迟是否是由等待外部响应引起的。

:( :(

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

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