'how to be sure that the visualization is updated with css changes via javascript?

example..

<input type="button" value="click me" id="p1">

<script>
 document.querySelector("#p1").onclick = function () {
    this.style.backgroundColor = "green";
    alert(`My background color is ${this.style.backgroundColor} ??`);
  }
</script>

The alert write My background color is green but at the moment of alert the color is gray!!

I know many ways to get around this:

..but is a way to be sure that the browser rendering is done?



Solution 1:[1]

You could use requestAnimationFrame - similar to setTimeout except the browser will take care of the timing as the function is run before a repaint.

A simple example:

function tellMe() {
  alert(`My background color is ${document.querySelector('#p1').style.backgroundColor} ??`);
}
document.querySelector("#p1").onclick = function() {
  this.style.backgroundColor = "green";
  requestAnimationFrame(function() {
    requestAnimationFrame(tellMe)
  });
}
<input type="button" value="click me" id="p1">

Solution 2:[2]

Here is the solution I found but doubt that this is the most elegant way of doing this:

OS is 5.4.0-26-generic #30-Ubuntu SMP Mon Apr 20 16:58:30 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

my .bashrc has (root has the same .bashrc)

source /opt/intel/oneapi/setvars.sh

created intel_libs.conf in /etc/ld.so.conf.d/ and added the line /opt/intel/oneapi/compiler/2022.0.1/linux/compiler/lib/intel64_lin this is where the libimf.so lives.

sudo ldconfig

compiled openmpi with intel compilers fine after that using:

./configure --prefix={HPCX_HOME}/ompi-icc CC=/opt/intel/oneapi/compiler/2022.0.1/linux/bin/intel64/icc CXX=/opt/intel/oneapi/compiler/2022.0.1/linux/bin/intel64/icpc F77=/opt/intel/oneapi/compiler/2022.0.1/linux/bin/intel64/ifort FC=/opt/intel/oneapi/compiler/2022.0.1/linux/bin/intel64/ifort  --with-ucx=/usr --with-platform=contrib/platform/mellanox/optimized
sudo make all 
sudo make install

I hope this helps someone else and please let me know if there is a better way of doing this. Cheers

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 A Haworth
Solution 2 theenemy