Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6672067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:27:52+00:00 2026-05-26T03:27:52+00:00

I have an event handler, but I want to make sure that it will

  • 0

I have an event handler, but I want to make sure that it will wait until all the other events in the event queue have executed.

For example, I have some tabs on a page. I have some general code for handling tabs that sets a tab to “Active Tab” when a user clicks on it.

$('a.tab').click(function() {
  $(this).parent('div').find('a.tab').removeClass('activeTab');
  $(this).addClass('activeTab');
});

For a specific set of tabs, I might have a handler:

// handler-1
$('div#myTabs a.tab').click(function() {
  do_something();
});

function do_something() {
  var type = $('div#myTabs a.activeTab').data('type');
  do_something_else(type);
}

do_something() might get called in response to other actions besides clicking on a tab, but regardless of how it gets invoked, it wants to know what the active tab is before it can continue.

My problem is that if it is getting invoked by the code under ‘// handler-1’, the class ‘activeTab’ may not have been reset yet, so the value it retrieves for ‘type’ will still be from whatever tag was selected prior to clicking on this one.

I solved it like this:

// handler-1
$('div#myTabs a.tab').click(function() {
  set_timeout( function() {
    do_something();
  }, 100);
});

This looks like too much of a kludge to satisfy me. How do I know that the timer event handler will get called only after all the other handlers have had their turn? Sure I could change “100” to “1000” or even “10000” but I would also be trading performance for more certainty (and there is still no certainty since even 10 seconds may not be enough on a browser instance on a machine that is crippled enough).

I have seen several possible solutions:

  • How to order events bound with jQuery
  • Binding multiple events in jQuery

but neither of these are general enough to satisfy me. I’m not trying to get a handler to run first. Rather, I want something like this:

function do_something() {
  $.when_all_other_events_have_been_handled(function() {
    do_something();
  });
});

Does anything like this exist?

I looked at $.fn.when(arg).done(), but when what? “arg” in this case would have to be a deferred object that executes whatever is already in the run queue. A Catch-22!

Sproutcore has something like “engine.run()” (don’t remember the exact name) that will return only when all other events in the “engine” have been handled, but I can’t find anything like that in jQuery. (http://api.jquery.com/category/events/). If anyone knows of something like that, please let me know!

Thanks! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:27:53+00:00Added an answer on May 26, 2026 at 3:27 am

    Try this:

    function postpone( fun )
    {
      window.setTimeout(fun,0);
    }
    

    so if you will call postpone(do_something); that do_something will be executed after all UI events that are in the event queue for the moment of postpone() call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an event handler that will remove an element from a list of
I have some event handler on a boundary class that manages a persistence mechanism
I have an event handler that needs to determine a type and execute code
In my Silverlight app I have an event handler that dynamically creates a new
I have the following: <select id=price_select></select> I have an event handler that invokes the
I have a simple event handler with a ItemAdding event that changes a column
I have a sharepoint event handler which I want to activate for a single
I'm a do it yourself kind of guy, but I want to make sure
I have added an EventHandler for the Click-event to a picturebox but on runtime
I have an event handler bound to the hover event using the .hover method,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.