Can't access object property, even though it shows up in a console log

后端 未结 30 1774
日久生厌
日久生厌 2020-11-22 06:26

Below, you can see the output from these two logs. The first clearly shows the full object with the property I\'m trying to access, but on the very next line of code, I can\

30条回答
  •  礼貌的吻别
    2020-11-22 07:15

    I've had similar issue, hope the following solution helps someone.
    You can use setTimeout function as some guys here suggesting, but you never know how exactly long does your browser need to get your object defined.

    Out of that I'd suggest using setInterval function instead. It will wait until your object config.col_id_3 gets defined and then fire your next code part that requires your specific object properties.

    window.addEventListener('load', function(){
    
        var fileInterval = setInterval(function() {
            if (typeof config.col_id_3 !== 'undefined') {
    
                // do your stuff here
    
                clearInterval(fileInterval); // clear interval
            }
        }, 100); // check every 100ms
    
    });
    

提交回复
热议问题