jQuery problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
htdung
Forum Newbie
Posts: 17
Joined: Sun Apr 27, 2008 6:35 am

jQuery problem

Post by htdung »

Dear all,
I'm a newbie in jQuery. I make some webs 2.0, included jQuery library and used $.ajax to send the request to server. I got a trouble : the cpu usage go high each time I click on the links which called the $.ajax, beside that the browsers(IE, FF3) spent high memory too. Was there anybody solve this problem ? Help.
Thank you all.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: jQuery problem

Post by Chris Corbyn »

You're really going to have to post some code. I can't even take a stab in the dark without seeing the code ;)

I've used jQuery.ajax without problem so I guess the problem is with your implementation :)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: jQuery problem

Post by Eran »

I make some webs 2.0
That's a classic
htdung
Forum Newbie
Posts: 17
Joined: Sun Apr 27, 2008 6:35 am

Re: jQuery problem

Post by htdung »

Dear Chris,
This is my site http://gtecgroup.nganphuongco.com/index.php.
You should open the task manager to see the problem I talked about. After each of click on the link in topmenu, the cpu usage go high and turn back to 0%. And, that number is higher and higher after I click. I don't know what is the issue.
Thanks for your help.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: jQuery problem

Post by Chris Corbyn »

I went to the site, couldn't see the word "Task Manager" anywhere, so I left. You really have to post the code here. People don't like running around much ;)

Just copy & paste your jQuery code here.
htdung
Forum Newbie
Posts: 17
Joined: Sun Apr 27, 2008 6:35 am

Re: jQuery problem

Post by htdung »

I mean you open the Task Manager of Window by click right mouse on your task bar and choose Task Manager . You can see the process iexplorer.exe . Your can set the Task Manager always ontop and click on the link in my site. You can see the CPU usage goes high each click.
Here is my code:

<script type="text/javascript">
function link(url,target){
$(target).html('<img src="templates/images/loading.gif">');
$.ajax({
url: url,
cache:false,
error:function(html){
$(target).html('The page is not exists !');
return false;
},
success: function(html){
$(target).html(html);
return false;
}
});
return false;
}

$(document).ready(function() {
$('a[name=ajax]').click(function(){
link(this.href,this.target);
return false;
});
link("home.php","#maincontent");
});

</script>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: jQuery problem

Post by Chris Corbyn »

CPU usage will always rise for a brief moment when the window redraws itself. I'm using Firefox on a mac and I don't see anything out of the ordinary ;)

What version of Internet Explorer are you using and what are the specs of your computer?

On a 2GHz Core 2 Duo with 2GB DDR3 RAM I see a brief moment of around 25% CPU use when I click the link, then it falls back down to around 10% (just idle) within a fraction of a second.
htdung
Forum Newbie
Posts: 17
Joined: Sun Apr 27, 2008 6:35 am

Re: jQuery problem

Post by htdung »

I don't agree with you. If the configuration of the computer is slow. The webpage will be slowly loaded. When you click on the links, they will be loaded with the same speed. I use AMD CPU Turion Dual Core 1.8GH, 1GB DDR2 Bus 800 MHz. But the website can get my computer slow down because it spend so much memory.
Do you have anysite using $.ajax to load pages, Chris ? Can I visit it ?
Thanks for your reply.
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: jQuery problem

Post by Chalks »

htdung wrote:I use AMD CPU Turion Dual Core 1.8GH, 1GB DDR2 Bus 800 MHz. But the website can get my computer slow down because it spend so much memory.
Honestly, that's a pretty slow computer, especially if you're running Vista on top of it. If you're just running XP, it's still pretty slow. If you have a bunch of programs running at startup, that will slow it even more.

Also, not only does your computer speed influence how fast the website loads, your internet speed does too. Additionally, the server speed makes a difference as well (at least in the case of AJAX). So if you're using a cheap/free server, have slow internet, and your computer is slow... of course it will appear as if ajax is running extra slow to you.
htdung
Forum Newbie
Posts: 17
Joined: Sun Apr 27, 2008 6:35 am

Re: jQuery problem

Post by htdung »

Chalks wrote:
htdung wrote:I use AMD CPU Turion Dual Core 1.8GH, 1GB DDR2 Bus 800 MHz. But the website can get my computer slow down because it spend so much memory.
Honestly, that's a pretty slow computer, especially if you're running Vista on top of it. If you're just running XP, it's still pretty slow. If you have a bunch of programs running at startup, that will slow it even more.

Also, not only does your computer speed influence how fast the website loads, your internet speed does too. Additionally, the server speed makes a difference as well (at least in the case of AJAX). So if you're using a cheap/free server, have slow internet, and your computer is slow... of course it will appear as if ajax is running extra slow to you.
Maybe my Eng is so pure, so all of you missed my mean. Althought your computers is slow or high configuration. I think the speed $.ajax send request to server is the same. My problem is that every time I click on the link the CPU usage get higher: 5% , 11% ,18%, 38% , 44% ...
I have change my code:
Every <a> tag before:

Code: Select all

 
<a href="abc.php" name="ajax" target="#maincontent">
 
Now:

Code: Select all

 
<a href="#abc.php" name="ajax" target="#maincontent">
 
And the JQuery code now:

Code: Select all

 
var currentAnchor = '';
 
$(document).ready(function(){
    $('a[name=ajax]').click(function(){
      target = this.target;
      if (currentAnchor!=document.location.hash){
        currentAnchor = this.href.substring(this.href.indexOf('#')+1,this.href.length);
        query = "";
      }
    $.ajax({
      url: currentAnchor,
      error: function (){
        $(target).html('The page is not existed !');
      },
      success: function(data){
             $(target).html(data);
          }
    });
        return false;
  });
});
 
Now my website is OK. I can click how many time on the link I wanna. The CPU usage is normal, the memory doesn't get higher. Is there anybody knows what my mistake before ? Tell me pls.
Really Thanks.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: jQuery problem

Post by pickle »

jQuery = Javascript = Client Side.

Moving to the appropriate forum.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply