Overview
I am working on a project and I ran into some problem in that things do not happen in the order in which I want them to happen. Therefore, I was thinking of creating some kind of queue that I can use to organize function calls and other various JavaScript / jQuery instructions used during startup, i.e. When loading the page. What I'm looking for does not have to be a queue data structure, but some kind of system that will ensure that actions are performed in that order and only when the previous task is completed can start a new task.
I briefly looked at jQuery Queue and AjaxQueue , but I really don't know how they work, so I'm not sure if this is the approach I want to take ... but I will continue to read more about these tools.
SPECIFIC
I currently set everything up so that some work happens inside $(document).ready(function() {...}); , and other work happens inside $(window).load(function() {...}); . For example,
<head> <script type="text/javascript"> // I want this to happen 1st $().LoadJavaScript(); // ... do some basic configuration for the stuff that needs to happen later... // I want this to happen 2nd $(document).ready(function() { // ... do some work that depends on the previous work do have been completed var script = document.createElement("script"); // ... do some more work... }); // I want this to happen 3rd $(window).load(function() { // ... do some work that depends on the previous work do have been completed $().InitializeSymbols(); $().InitializeBlock(); // ... other work ... etc... }); </script> </head>
... and it's really tiring and ugly, not to mention poor design. Therefore, instead of dealing with this mess, I want to create a fairly universal system so that I can, for example, enqueue $().LoadJavaScript(); , then var script = document.createElement("script"); , then $().InitializeSymbols(); , then $().InitializeBlock(); etc .... and then Queue will execute function calls and instructions, so that after completing one command the other can start until the queue is empty, instead of calling dequeue again.
The rationale for this is that some work must be performed, such as configuration and initialization, before other work begins due to the dependence on the configuration and initialization steps taken. If this doesn't seem like a good solution, let me know :)
SOME MAIN WORKS
I wrote the code for the basic queue, which can be found here , but I want to expand its functionality so that I can store various types of "Objects", for example, separate JavaScript / jQuery instructions and function calls, mostly code fragments that I want to execute.
UPDATE
In the current Queue implementation that was implemented, it looks like I can store functions and execute them later, for example:
// a JS file... $.fn.LoadJavaScript = function() { $.getScript("js/Symbols/Symbol.js"); $.getScript("js/Structures/Structure.js"); }; // another JS file... function init() { // symbols and structures }; // index.html var theQueue = new Queue(); theQueue.enqueue($().LoadJavaScript); theQueue.enqueue(init); var LJS = theQueue.dequeue(); var INIT = theQueue.dequeue(); LJS(); INIT();
I also think I figured out how to store separate instructions like $('#equation').html(""); or perhaps even if-else statements or loops, by transferring them as such:
theQueue.enqueue(function() { $('#equation').html("");
But this approach would require me to wait until the line is completed with his work, before I can continue to do my job. This seems to be the wrong design. Is there a smarter approach to this? Also, how can I find out that a particular function has completed execution so that the Queue can know in order to move on? Is there some kind of return value that I can wait for or a callback function that I can specify for each task in the queue?
WRAP-UP
Since I am doing everything on the client side, and I can not get the Queue to do its own thing independently (according to the answer below), is there a smarter decision than me, just expecting the queue to finish its work
Since this is more a design question than a specific code question, Iām looking for suggestions on how to solve my problem, tips on how I should design this system, but I definitely welcome you and would like to see code to support the suggestions :) I also I welcome any criticism regarding the Queue.js file that I linked to above, and / or my description of my problem and the approach that I plan to take to solve this problem.
Thank you Christo