javascript - jQuery: how to check to make sure all asynchronous actions have been completed -
i have web form bunch of checkboxes; when specific box clicked, shows/ hides specific div more data in it. when divs hidden/ shown, run formatting command organize , color them properly.
some checkboxes dependent on others, , cascading scheme implemented, possible, likely, several checkboxes changing state @ 1 go.
instead of writing code each checkbox, decided take shortcut , handle checkboxes same way. ended (using 2 checkboxes/ divs make point, there's bunch more):
jquery(document).ready(function () { alternatedivs(); jquery('#accesscas, #cas_lelocspecific').on('click', function () { showhide(); }); }); function showhide() { var thechecks = [ { checkid: 'accesscas', divid: 'divcas' }, { checkid: 'cas_lelocspecific', divid: 'divleloc' } ]; var pendingchanges = 0; (i = 0; < thechecks.length; i++) { var checked = jquery('#' + thechecks[i].checkid).is(':checked'); var visible = jquery('#' + thechecks[i].divid).is(':visible') if (checked && !visible) { pendingchanges++; jquery('#' + thechecks[i].divid).fadein(400, function () { pendingchanges--; }); } else if (!checked && visible) { pendingchanges++; jquery('#' + thechecks[i].divid).fadeout(400, function () { pendingchanges--; }); } } //check pending changes } function alternatedivs() { jquery('.formtable:visible:odd').css('background-color', '#eeb'); jquery('.formtable:visible:even').css('background-color', '#eef'); }
it's necessary me make sure divs supposed show show properly, means must transitioned visible/ not visible state before calling alternatedivs()
. can see set pendingchanges
value incremented , decremented divs transitioned.
what i'd set asynchronous process check when pendingchanges
equals zero, , when happens invoke alternatedivs()
. want asynchronous not interfere user experience.
i believe can done through sort of callback, out of expertise area. how set , invoke asynchronous function check make sure other asynchronous functions completed, , when call function , exit?
i hope that's clear.
how putting if statement in fade callbacks?
if(pendingchanges==0){ alternatedivs() }
Comments
Post a Comment