-1

Calling loadLevel1Categories from checkSessionCount and then do something to the all .alvl1 classes. I know the data is being returned as I can see it. It seems that the Defer isn't working.

I've tested by putting a setTimeout just after the loadLevel1Categories(); and everything works that way, but that's messy and there will be different amounts of data being returned so cannot predict how much of a delay to add.

$(function(){
    
    var objCats = $('#portal_categories');
    
    function loadLevel1Categories() {
        
        objCats.empty();
        
        var dfd = $.Deferred();
    
        $.getJSON('getdata.php?ty=ca1&ts='+new Date(),function(){
            //success
        }).done(function(data){
            
            var r = new Array, j = -1;
            $.each(data,function(k,item) {
    
                r[++j] = '<a href="#" class="alvl1" data-id="'+item.id+'">'+item.title+'</a>';
                
            });
            
            objCats.html(r.join(''));
            
            dfd.resolve();
    
        }).fail(function(){
            objCats.html('Unable to locate any data');
        }); 
        return dfd.promise();
    }
     
    function checkSessionCount() {
    
        $('#debug').empty();
        loadLevel1Categories();
    
        //do something with the .alvl1 elements once data is returned
        //but this bit isn't working as the database hasn't been returned yet
        
    }

    //on page load
    checkSessionCount();
    
});
Martin
  • 240
  • 4
  • 13

1 Answers1

0

you can use .then callback on

loadLevel1Categories().then(()=>{
   //do something with the .alvl1 elements once data is returned
})
Aakif
  • 156
  • 6