0

I have several links for sorting current elements on page.

Functions:

  • A - click handler with array sort
  • B - display elements according to sorted array.

Sorting can take even 2 seconds, so I want to display some feedback to user.

I thought about change link text to 'In progress..' or something like that.

In fun A store old link text and change it to 'In progress' and pass another function to B that I invoke after display elements to show old text of link, but it just doesn't work.

In debugger link text is changed, but I can see only old text all the time.

How can I create valid callback for this?

I see websites with similar sorting and without any feedback to user too.

Is this possible to do with javascript? Or feedback can be display only on ajax request?

1
  • 1
    mhm... if you change the link text before starting the sort imho this should work. Can you post some code snippet? Commented Aug 26, 2011 at 14:28

1 Answer 1

3

The issue is most likely that you're running entirely synchronous code.

The DOM does not update until whatever function(s) you've started have completed (even if you update the DOM before the long running calculation).

Here's an example of a long running calculation that prevents the DOM from updating immediately. (Click the "Render" button at the top to see the example properly.)

Notice in the example that the "Starting... Wait a few seconds." text appears immediately, and is not blocked by the long running calculation. But the "Updated" text takes 3 seconds to appear.

The reason for this is because I added the "Starting..." to the DOM first, then wrapped the rest of the code in a setTimeout. This allowed the DOM to update immediately for "Starting...", but the rest of the code within the setTimeout is synchronous, so the DOM won't update again until that code is complete.


So your solution can be to update the text with "In progress...", then wrap the rest of the code in a setTimeout() in order to allow the DOM to update immediately with your progress message.

element.innerHTML = "In progress...";

setTimeout( function() {
    // invoke the sorting code
}, 0);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.