How to check if a variable is loaded in JavaScript?

后端 未结 10 1686
遇见更好的自我
遇见更好的自我 2021-01-31 04:01

How do I see if a certain object has been loaded, and if not, how can it be loaded, like the following?

if (!isObjectLoaded(someVar)) {
    someVar= loadObject()         


        
10条回答
  •  孤街浪徒
    2021-01-31 04:41

    You probably want to see if a given object is defined

    Especially if its done in an asynchronous thread with a setTimeout to check when it turns up.

      var generate = function()
      { 
          window.foo = {}; 
      }; 
      var i = 0;
      var detect = function()
      {
         if( typeof window.foo == "undefined" ) 
         {
               alert( "Created!"); 
               clearInterval( i );
         }
       };
       setTimeout( generate, 15000 ); 
       i = setInterval( detect, 100 ); 
    

    should in theory detect when window.foo comes into existance.

提交回复
热议问题