Search code examples
javascriptecmascript-5

javascript - var in method call not redefined


I've written a code which works perfectly but when I switch hardcoded parameters to a variable I get stuck with a problem which I understand but I don't have an idea how to resolve. Also, I am not aware if there is any kind of pattern which could resolve this.

var IsIframeAdded = (function(){

    var nr_of_elem = 0;
    var elem;

    events.subscribe( elem + "Loaded", function(length){
        disableIframes(elem);
        nr_of_elem = length;
    });
    //events.subscribe() must be placed above checkIfElemAdded()


    var checkIfElemAdded = function() {
        elem = functional.scripts[0].class;  //Change-set the value of elem variable

        if ( is_page() || is_page('shop') || is_page('portfolio') || is_page('fl-studio-song-starter') ) {
            domCallback.isElemAdded( elem, nr_of_elem ); //check if iframe is added to the DOM
        }
    };


    events.subscribe( "JSONloaded", checkIfElemAdded );
    events.subscribe( "loadMoreClicked", checkIfElemAdded );

})(); // !isIframeAdded


$('.music-portfolio-query').on('click', '.showmore-btn', function(){
    events.publish( "loadMoreClicked" );
});
    enter code here

As I have marked with a comment in code I must keep events.subscribe() above function called checkIfElemAdded. That way I avoid using setTimeout() on response from domCallBack.isElemAdded()

I have mentioned that I understand the problem so I will try to describe it:

Events.subscribe() doesn't listen to the correct event name.

Problem is with elem variable in events.subscribe() method call That variable is set on script load which is too early for variable to be set with an element that is pulled from JSON file. Variable elem in that call should be loaded after JSONloaded event. ( I am trying to avoid setTimeout solutions )

Are there any developers experienced enough to help me out with this?

Thanks


Solution

  • I have found a solution myself so if anyone would need it, here it is.

    var IsIframeAdded = (function(){
    
        var nr_of_elem = 0;
        var elem;
    
        var checkIfElemAdded = function() {
            elem = functional.scripts[0].class;  //Change-set the value of elem variable
    
    
            events.subscribe( elem + "Loaded", function(length){
                disableIframes(elem);
                nr_of_elem = length;
            });
            //events.subscribe() must be placed above domCallback.isElemAdded()
    
    
            if ( is_page() || is_page('shop') || is_page('portfolio') || is_page('fl-studio-song-starter') ) {
                domCallback.isElemAdded( elem, nr_of_elem ); //check if iframe is added to the DOM
            }
        };
    
    
        events.subscribe( "JSONloaded", checkIfElemAdded );
        events.subscribe( "loadMoreClicked", checkIfElemAdded );
    
    })(); // !isIframeAdded
    
    
    $('.music-portfolio-query').on('click', '.showmore-btn', function(){
        events.publish( "loadMoreClicked" );
    });