In several cases I need to wait for the browser to repaint and execute a function after. Ex: show alert after loading animation gets hidden.
This works best so far:
loader.classlist.add('hidden');
requestAnimationFrame(() => {
setTimeout(() => {
alert(res);
});
});
What also works is a simple "setTimeout()" but it's hard to determine the best amount of delay since it varies. Sometimes 10 is too long, sometimes too short. In case the above solution is best and since I'll need to call this several times for different functions: How can I wrap this into a function (async?)
function timeOut () => {
requestAnimationFrame(() => {
setTimeout(() => {
return true;
});
});
}
timeOut(); if timeOut returns true then go on, alert(res) or execute other function