2

I'm lazy loading part of my code, some file containing classes that are then called in other parts, so I wrote a simple code that checks if a class instance exists and then execute it, or retry after 100ms.

let checkExist = setInterval(function() {
    if (typeof(ClassInstanceName) === "object") {
       ClassInstanceName.DoSomething();
       clearInterval(checkExist);
    }
 }, 100);

This code works, but I need to generalize it in a self contained function

 function WaitForClass(args) {

    let instance    = (args.instance    === undefined ? "" : args.instance);
    let callback    = (args.callback    === undefined ? "" : args.callback);

    if (instance == "" || callback == "") {
        console.log("Error -> " + instance + " " + callback);
    }
    else {
        if (document[instance]) {
            //it never find the instance
            callback();
        }
        else {
            setInterval(function() {
                WaitForClass(args);
            }, 100);
        }
    }

 }

 WaitForClass({instance:"ClassInstanceName", callback:function(){ ClassInstanceName.DoSomething();}});

The function is simple, but can't make it work. For compatibility with other code portions I need to check if the instance exist using a string containing the name, and not just the object representing it.

5
  • 1
    What is supposed to do document[instance] ? What about typeof instance === 'function' ? Anyway you have to do setTimeout(function() { WaitForClass(args)}, 100) and not setInterval Commented Nov 28, 2018 at 17:35
  • 1
    I've tried many methods included typeof, but I need to pass a string containing the name of the instance and not the instance itself, so typeof always returns "string". The document[] method was the last attemp. Commented Nov 28, 2018 at 17:43
  • Do you know all the possible classes which could be loaded in your app or is it fully dynamic? Anyway you have to reference your classes somewhere and link them to their string name : see stackoverflow.com/questions/34655616/… Commented Nov 28, 2018 at 17:46
  • Dynamic, but can modify them. Your question prompted me that in the constructor they can signal their existence to a global array, so that I can check that instead. It's a solution, but I would prefer a more standard way to not incur in problems in the future. Commented Nov 28, 2018 at 17:51
  • 1
    In the end I solved with this if (typeof(window[instance]) === 'object') {. I was circling around it XD Commented Nov 29, 2018 at 9:27

1 Answer 1

0

You can check whether a Class (ES6+ Class, not css class) is available in your environment by:

    if (typeof MyClass==="function")
    {
        // MyClass is available
        console.log("MyClass is available")
    } 
    else 
    {
    // MyClass is not available
  console.log("MyClass is not available")
    }

Type of Class is function, and type of function is also function, and functions are objects in JavaScript.

And if you don’t like the idea of checking type of a Class to be equal to function, you can do this.

try{
  let MyInstance =new MyClass();
    
        // MyClass is available
        console.log("class available")
    } 
  catch(e) 
    {
    // MyClass is not available
    console.log("class not available")
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.