Javascript method priority queue

Problem Statement:

There are times when people are faced with the problem of calling methods in a particular order based on some kind of priority. There are times when you wish to be able to add methods to some kind of a method queue along with the priority. The priority decides which methods is to be called first. Then at some later point, you wish to execute the methods in the queue in order of priority.
A case in point may be when you have multiple document ready handlers which add method calls to the queue, and then on the window.load event you execute all the methods.

So keeping the queue thing in mind I have come up with a solution to this problem.


Function_queue = new function()
  // lower priority value means function should be called first
  var func_queue;

  var sort_queue = function(a, b)
    if( a.priority < b.priority )       return -1;
    else if( a.priority == b.priority ) return 0;
    else                                return 1;

  return {
    init : function()
      func_queue = new Array();

    * @param function func This can be a function name or a function itself
    * @param array|null args This should always be an array of args or a
    *	null if the function accepts no arguments
    * @param int priority This is a zero based number, with the smaller
    *	value meaning high priority. Function with a priority of 0 will
    *	be executed before a funcion with priority of 2
    add_to_queue : function(func, args, priority)
        method   : func,
        args     : args,
        priority : priority
    execute_queue : function()
      func_queue.sort( sort_queue );

      for( var i in func_queue ) func_queue[i].method.apply( null, args );


  // initialize the queue

  $( document ).ready(function()
    // add a function to queue that does not accept any parameters
      alert( 'Hello World 1' );
    null, 2);

  $( document ).ready(function()
    // add another method to queue that accepts two integers as parameters
    Function_queue.add_to_queue(function(a, b)
      alert( 'Sum = ' + ( a + b ) );
    new Array( 3, 4 ), 1); 

  $( window ).load(function()
    // now all you have to do is execute the queue which will do all the magic

You may download the script from here.

Ovais is a storage architect with keen focus on reliability, efficiency and performance of OLTP databases, more specifically MySQL. He currently works at Uber on ensuring storage reliability and scalability. Previously, he helped different web properties scale their MySQL infrastructure. He also worked at Percona as a Senior MySQL consultant and at a few other startups in various capacities.

  • This was mentioned here

  • This was mentioned here

  • James Fox

    Very nice

  • James Fox

    Very nice

  • Oakley vault

    The eyewear brand companies Oakley vault sunglasses mainly for women with a couple of designs in the actual mens category. Known because of its colorful and unique sets, Oakley vault are easily recognizable using the embossed logo on the actual temples. The exquisite design is from the frames range from Oakley sunglasses outlet to classic aviator tones, with pastel and earthy colored lenses. The variety of colors the brand offers could be compared with the mood from the Cheap Oakley vault.

  • Coach Factory Outlet,

    If you are a Coach fan like everybody, then
    chances are you know that the best place to find Coach Outlet
    Products at a super discount is at
    one’s own
    Although there are some local shops that are discounting ever more so they can
    go inventory. Believe it! You can save a handsome profit at
    in amazing products. If you didn’t
    made it out to one of their Coach Outlet
    or discount retail stores, all you
    need to try and do is check online for
    or outlet malls. You’ll soon discover a lot of specialty stores dedicated to
    supply discount Coach
    where they sell only genuine
    Discipline items and accessories.

    The Coach
    internet is also a good resource for
    finding low priced Coach handbags, purses, trainers, and accessories. In simple
    fact, I personally prefer to continue online for great
    Coach Outlet Online
    buys rather than raiding the high-end stores around my neighborhood. As an
    icing on the cake, you’ll get your product shipped right to your doorstep
    instead of having to pay for gas expenses drive an automobile to the retail
    store orders
    . Hope you have great memories shopping
    from my home
    within a great discount just when I have!