I use the async.js library from Caolan McMahon and jQueryUI to provide user feedback, while several asynchronous calls collect data and populate the elements of a complex graph.
My question is: What is the best way for data areas to be used by asynchronous methods?
This is a simplified example of what I'm doing. I have work using global variables, but they bother me a bit and complain about jsLint. Passing arguments or defining an area in document-ready mode interrupts it.
Analogs to updateA() et. and others in my real code are hundreds of lines and include XHR calls.
JavaScript:
// global variables. Bad? var steps = 3; var ticked = 0; var otherCounter = 0; $(function() { $('#progressbar').progressbar({ value: 0 }); async.parallel([ function(onDoneCallback) { updateA(onDoneCallback);}, function(onDoneCallback) { updateB(onDoneCallback);}, function(onDoneCallback) { updateC(onDoneCallback);} ], function(err, results) { // final callback when they're all done tickProgress('All done after ' + ticked + ' ticks.', true); }); }); function tickProgress(message) { var curvalue = $('#progressbar').progressbar('option', 'value'); var done = false; if (arguments.length > 1) { done = arguments[1]; } $('#progress_text').html(message); if (done) { $('#progressbar').progressbar('option', 'value', 100); } else { $('#progressbar').progressbar('option', 'value', curvalue + 100 / steps); } ticked++; // global OK here? } function updateA(onDoneCallback) { setTimeout(function() { $('#a').html('A is foo. otherCounter ' + otherCounter); tickProgress('updated A at otherCounter ' + otherCounter); otherCounter++; onDoneCallback(null, 'A done'); }, 1000); } function updateB(onDoneCallback) { setTimeout(function() { $('#b').html('B is bottle. otherCounter ' + otherCounter); tickProgress('updated B at otherCounter ' + otherCounter); otherCounter++; onDoneCallback(null, 'B is OK'); }, 100); } function updateC(onDoneCallback) { setTimeout(function() { $('#c').html('C is cauliflower. otherCounter ' + otherCounter); tickProgress('updated C at otherCounter ' + otherCounter); otherCounter++; onDoneCallback(null, 'C done'); }, 2000); }
HTML:
<p id="progress_text" style="background:yellow">Loading...</p> <div id="progressbar"></div> <hr /> <h2>a</h2> <p id="a">Looking up a...</p> <h2>b</h2> <p id="b">Looking up b...</p> <h2>c</h2> <p id="c">Looking up c...</p>
Violins:
I have sample code in JSFiddle if you want to pounce on it.
source share