0

I was using JQuery code on my page to send a user's selection from a drop-down to the server. For some reason when I used .live() like this:

$(".rNameSelect:not(.srchCntnt .rNameSelect)").live("change", function(){
        var rName=$(this).val();
        $("#managersViewTabs .mgrCntnt").load("managerview.do?type=report&reportName=" + encodeURIComponent(rName));
    }); 

the page got progressively slower each time I clicked on another choice in the drop-down! When I changed it like this:

$(".rNameSelect:not(.srchCntnt .rNameSelect)").change(function(){
        var rName=$(this).val();
        $("#managersViewTabs .mgrCntnt").load("managerview.do?type=report&reportName=" + encodeURIComponent(rName));
    }); 

the problem was solved! Why is .live() slow? Did I really want .live() or is .change() okay to use?

elizabk
  • 480
  • 2
  • 11

2 Answers2

1

When you'r using .live() it will work like a trigger and the script will need keep runing until this action happens. And it'll consume RAM gradative.

When you use .change() on that case, the jquery will assume this change on native javascript event. And it'll be lot more faster.

Take a look here and here to understand better how events work on jquery.

Community
  • 1
  • 1
Guerra
  • 2,792
  • 1
  • 22
  • 32
0

.live() method slow since it is only used to register the event handler on the document. Your events always delegate all the way up to the document. This can affect performance if your DOM is deep.

My suggestion, use .on() method because you setting the event handler in the part what that you want.

saeta
  • 4,048
  • 2
  • 31
  • 48