Wait css loaded to execute JS - yarn start


#1

Hi,

I’m currently on a project in development but I need to make for example this thing on JavaScript

var myWidth = $(element).width();

The problem is in development mode (yan start), the css is loaded by JS and my variable return to me the size of the browser because when I get my var, the CSS isn’t loaded yet.

But if I do this

setInterval(function(){
    var myWidth = $(element).width();
  }, 2000);

With that delay, I have the correct value.

There is a solution for that ? Any event listener or something else ?


#2

I made something like that and it’s work but maybe it’s not the right method ?

var checkLoadingInterval = setInterval(function(){
  if(getComputedStyle(document.body).boxSizing == 'border-box'){
    clearInterval(checkLoadingInterval);
    // Load Events
    jQuery(document).ready(() => routes.loadEvents());
  }
}, 100);

// Load Events
// jQuery(document).ready(() => routes.loadEvents());