views:

110

answers:

2

Hi all. I'm sorting a list of species, but sometimes the list is large and it takes several seconds to sort it. Thus, I tried adding a small notice to let our users know there's something running (and they should wait). Please just check this page: http://ibc.lynxeds.com/family/babblers-timaliidae And click on "Sort and Filter [+]". Then, click on any "Sort by" link.

With Opera, you will first get a message, then it starts sorting the species, then the message changes to a different text and that's all. With FF or IE, the first message isn't displayed, and you just know the CPU is doing some work, then the "Done!" message is shown and fades out.

So, in the worst case (slow CPU, large list), you might be waiting for 20 seconds and then you get that Done! message (it would be much better to show the "please wait" first, and just fade out when done...)

The JQuery code that calls the sort function is this:

$(".s_alf_eng").toggle(
 function() {
   $(".warning").text("Sorting... please wait").show(); //warning is an empty div
   $('.media-status-specie li').sort(sortAscending1).appendTo('.media-status-specie');
   $(".warning").text("Sorting... Done!").fadeOut(700);
      },
 function() {
   $(".warning").text("Sorting... please wait").show();
   $('.media-status-specie li').sort(sortDescending1).appendTo('.media-status-specie');
   $(".warning").text("Sorting... Done!").fadeOut(700);
      });

It works perfect in Opera. I don't understand why in the other browsers the first message is not shown.

What can I do to force FF and IE to display the "Please wait" message before starting the sort?

A related question is how to speed up the sorting (it can take up to 20 seconds for the largest family, in my core2duo...).

Thank you for any suggestion!

EDIT: The sort function is found here: stackoverflow.com/questions/1531176

A: 

Firstly, what are you using to sort i.e. what is in sort()?

Secondly, is there a callback function that can be used to set the "Done!" message after the sort has finished?

Thirdly, the sort may be able to be sped up, depending on the sorting algorithm used in sort(). If you can provide more details, I should be able to help further.

Russ Cam
I'm adding a new question regarding the speed of the sort function. I'll be able to post it in 15 minutes :)I'm not sure about the second question. Could you be more specific? Thanks!
Ferran Gil
It is more important to show the "Sorting, please wait" message rather than the "Done" one. The "done" could be removed. The first one if much more vital, or users would click again, and again..
Ferran Gil
most plugins allow you to specify a function to be executed when a function completes. This is generally known as a callback function. So, if `sort()` takes a callback function, it would be in this function that you would want `$(".warning").text("Sorting...Done!").fadeOut(700);`
Russ Cam
With a callback function, you could also disable the ability to sort/filter until `sort()` has finished.
Russ Cam
Looking through your script in the URL you've provided, there are a number of things that you could do to improve the speed and maintainability. Only one `$(document).ready()` is needed (although having more than one is fine, albeit more difficult to read/maintain). I can see that that the `sorting...` message is on a click event handler and not part of the `toggle()` functions for each element the user can sort by.
Russ Cam
Also, I would advise better qualifying your selectors as using `class` names only means that each element will need to be checked to see if it has class x, whereas using, for example, `div.x` means that only div elements will be checked to see if they have class x
Russ Cam
Yes, it was on the toggle() part, but as you can sort up and down, I had twice the lines. That is why I moved the first part (Sorting...please wait) "outside", with a click. I'll move everything into one single $(document).ready(). Functions like jQuery.fn.sort = function() { return this.pushStack([].sort......... must be placed on the same .js file, but after closing ready(), right?
Ferran Gil
`jQuery.fn.sort...` does not need to be in `$(document).ready(...` nor in the same js file. If it is going in a spearate js file, then that file will need to be referenced after the jQuery js file. I would be tempted to add a callback function to `jQuery.fn.sort...` for the reasons discussed and use this to control the "Done!" message and re-enable users to sort.
Russ Cam
+1  A: 

Just as a test, what happens if you change:

$('.media-status-specie li').sort

to

$('.media-status-specie').find('li').sort....

or, better yet:

<ul id="media-status-specie" class="media-status-specie">

and then:

 $('#media-status-specie').find('li').sort.....

Just as an FYI, I get a browser warning message about slow running script, I let it run and it does finish. It is several seconds before I see the warning message you post by the way.

Mark Schultheiss
I'll try it in local, but what is the expected result?
Ferran Gil
Using a ID is much faster and restricts the select. The idea is to keep from walking the entire DOM.
Mark Schultheiss
You might also get some boost in speed by selecting the context.See Brandon Aarons blog for some details on context selection:http://brandonaaron.net/blog/2009/06/24/understanding-the-context-in-jquery
Mark Schultheiss
Thank you for that link. I now understand what the context is.
Ferran Gil
I was getting the same results using the id, but then I saw I needed to use the #media.. also on the appendTo() function! From 22 seconds to just 5!!! This is great, and very easy to deploy :) Thanks!
Ferran Gil
I'm using a new method to get even better results! Went down from 5 to 2 seconds! The idea is walk the entire DOM once and get the "important" (from a sort point) values, and then use that ones, no need to browse through the DOM.http://stackoverflow.com/questions/1531176/jquert-sort-function-performance/1531273#1531273
Ferran Gil