简体   繁体   English

如何提高jQuery的性能

[英]How to increase performance of jQuery

i have an chat application written in jsp and jQuery. 我有一个用jsp和jQuery编写的聊天应用程序。 I have used the setTimeout method for threads who are continuously posting request in some URL. 我已将setTimeout方法用于在某些URL中连续发布请求的线程。 My browser is hanging after some time and cpu utilization is very high. 我的浏览器在一段时间后挂起,CPU使用率很高。

Is there any way to increase performance of JS? 有什么办法可以提高JS的性能?

Check this one: http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx#tip1 检查这一点: http : //www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx#tip1

It has some really good recommendations like: 它有一些非常好的建议,例如:

  • Keep selection operations to a minimum by caching 通过缓存将选择操作减至最少
  • Keep DOM manipulation to a minimum 尽量减少DOM操作
  • Use IDs instead of classes wherever possible 尽可能使用ID代替类
  • Give your selectors a context 给选择器一个上下文

Have you tried debugging with Firebug ? 您是否尝试过使用Firebug进行调试? You can use it to debug javascript, monitor requests and responses, and more. 您可以使用它来调试javascript,监视请求和响应等等。

Well i run trough this before here is what i did (by the way this has nothing to do with jquery) : 好吧,我在这之前做过了(这与jquery无关):

try {
    window.clearInterval(x);
} catch(e) {
    // do nothing
}
x = setInterval(Update, 1000);

in your case you should clearTimeout :) 在您的情况下,您应该clearTimeout :)

try this : 尝试这个 :

refresh() { $.post("./chatServlet",  {  message: "hxci",  name: $("#author").val(),  event: "AUTOROOMMESSAGE",   roomname: $("#select").val() },   function(xml)   {   $("#msg").empty();   addMessages(xml);    });    try {    window.clearInterval(x);}    catch(e) {       }     x = setInterval(refresh, 3000);   }

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

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