I have a really long application so i’m trying to minimiza cpu/memory usage
with each function, therefore I have a few simple questions.
-
on elements added after the page has been loaded,
is there a difference between putting “onclick” inside the element tag or using
jquery’s delegate like the following example?
$(“#mainchatwindow”).delegate(‘img[func=“gift”]’,‘click’ function… -
does it make a big difference selecting an element with a full path
like (#body #contentdiv #userstbl td.example)
or just typing (.example) ? -
I have 2 arrays of objects, both objects has an id attribute, one of the
objects are the images on the page, the second one is an ajax result with updated images.
will it be better to first loop threw both objects just to create new arrays
with just the id’s instead of looping the threw those big objects?
like this:
if (data.online)
var online_length = data.online.length;
else
var online_length = 0;
var online_users = []; //user
var online_users2 = []; //server
$.each($("#userwindow .online_users"), function(i, v) {
online_users.push($(v).attr('fb_id'));
});
for (i=0; i<online_length; i++) {
online_users2.push(data.online[i].fb_id);
};
// check for new online users
for (i=0; i<online_length; i++) {
if(jQuery.inArray(data.online[i].fb_id, online_users) < 0)
$("#userwindow #online_users").append('<a target="_blank" href="http://www.facebook.com/profile.php?id='+data.online[i].fb_id+'"><img title="לחץ לכניסה למשתמש" border="0" class="online_users" fb_id="'+data.online[i].fb_id+'" width="40" height="40" style="margin:2px;" src="http://graph.facebook.com/'+data.online[i].fb_id+'/picture" /></a>');
}
// check to remove no logner onnline users
for (i=0; i<online_users.length; i++) {
if(jQuery.inArray(online_users[i], online_users2) < 0)
$("#userwindow .online_users[fb_id='"+online_users[i]+"']").parent().remove();
}