I have a site that gets a user data on logging in. this data is saved by mysql as a string, and after calling this string with ajax, I split the data by js function: str.split(). this returns an array with all the id's of the btns I would like to check, and with the ".each" call I check all matched btns. the big problem is that this process takes about 10 seconds on local server. is there a better and faster way to get user data? p.s: we talk about hundreds of btns on user login.
here is the code:
function get_saved_list(){
$.ajax({
type: "post",
url: "ajax/get_saved_list.php",
success: function(data){
list=data.split(";");
for(j=0; j<list.length-1; j++){
$(".content #"+list[j]).siblings('a').trigger('click');
}
setTimeout("$('#save').hide()", 1);
}
});
}
some of the html include the 'a' that is triggered:
<ul class="content"><li>
<a class="third_cat_chb cat_chb_off"></a><span>page</span>
<button id="page_<?php echo $row['page']."Z"; ?>chapter_<?php echo $row['s_id'];?>" style="width: 20px; margin-right: 5px;" class="page_chooser"></button></li></ul>
sory for the bad format : )
Don't prefix your ID selector with a class. This prohibits jQuery from using the native getElementById(), and instead uses getElementsByClassName(), which is slower.
Try this selector instead: $("#"+list[j])
I was able to loop through 1000 items and trigger a click in about 1 second, rather than 4-5 seconds by doing a class first.
It would also help if you could put an ID on the actual <a>
that you want to click so that you can avoid the call to siblings(), which adds additional overhead.
If you've got control over the server it would save a huge amount of time to use the checked="true"
html attribute, setting it conditionally in php instead of triggering a click event.
If you're stuck with Javascript: I'm not sure, but it might also be faster to set the checked
property than to click it, since you wouldn't then also be triggering a "click" event.
$("#"+list[j]).siblings('a').attr('checked');
should work, although I haven't tested it.
Really though, if you can, setting it on the server is the way to go.