Looking for an explanation rather than a solution.
I'm facing a situation where updating the selected value and then refreshing the selectpicker as per this SO post suggest doesn't work properly.
Here is my code:
$('[name="city"]', form).val(subscription.city || ''); // <input>
$('[name="province"]', form).val(subscription.province); // <select>
$('[name="locale"]', form).val(subscription.locale || defaultLocale); // <select>
$('select', form).selectpicker('refresh');
Strangely on some system the last statement setting doesn't pick the value set for locale
. Encapsulating the last statement within a setTimeout
solve the problem.
setTimeout(function(){
$('select', form).selectpicker('refresh');
}, 10);
I am aware JavaScript is event driven and an asynchronous language, but doesn't basic statement like those in my example gets executed synchronously?
Although it solves the issue, I find this "hacky" as I have dozens of places where this type of situation occurs.
Update
I discovered that encapsulating the statement in a setTimeout actually didn't always worked.
But, refreshing the page while holding the Ctrl key, worked. Something I have to figure out. I was wrong.
I also found the reason for the "on some system". The system showing the misbehaviour is the one for the production environment. Why this one? Because on this system there are other script being loaded for analytics and marketing strategy.
I found a script from MsDynamics when commented/omitted, the misbehaviour above is gone and all works good. I now need to find out what in this piece of code is messing with mine.