javascript - Out of order execution - no Ajax involved -
i'm experiencing out-of-order execution in javascript, , it's not related ajax calls or such. main mass of code perchance slow dom manipulation, followed method call. in every single case, function phone call beingness fired before dom manipulation finishes.
here code:
$(this).parents('dd').siblings('dd').each(function(){ var filter_name = $(this).attr('data-filter-type'); if ($(this).hasclass('selected')) { $(this).removeclass('selected', function(){ if ($(this).hasclass('date')) { $('form[name="filter-form"] input[name="from"]').remove(); $('form[name="filter-form"] input[name="to"]').remove(); } else { $('form[name="filter-form"] input[name="' + filter_name + '"]').remove(); } console.log('removed'); }); } }); var filter_type = $(this).parents('dd').attr('data-filter-type'); var filter_input = 'form[name="filter-form"] input[name="' + filter_type + '"]'; if ($(filter_input).length > 0) { $(filter_input).val(filter_value); } else { $('form[name="filter-form"]').append('<input type="hidden" name="' + filter_type + '" value="true">'); } dostuff($(this)); in console, seeing result of dostuff before seeing debug.
anybody have ideas how create function phone call wait?
there overload of .removeclass takes function, it's not callback function (that operation completed synchronously). i'd recommend removing function argument removeclass , placing code after call:
$(this).parents('dd').siblings('dd').each(function(){ var filter_name = $(this).attr('data-filter-type'); if ($(this).hasclass('selected')) { $(this).removeclass('selected'); if ($(this).hasclass('date')) { $('form[name="filter-form"] input[name="from"]').remove(); $('form[name="filter-form"] input[name="to"]').remove(); } else { $('form[name="filter-form"] input[name="' + filter_name + '"]').remove(); } console.log('removed'); } }); var filter_type = $(this).parents('dd').attr('data-filter-type'); var filter_input = 'form[name="filter-form"] input[name="' + filter_type + '"]'; if ($(filter_input).length > 0) { $(filter_input).val(filter_value); } else { $('form[name="filter-form"]').append('<input type="hidden" name="' + filter_type + '" value="true">'); } dostuff($(this)); javascript jquery
No comments:
Post a Comment