0

I've created a function to iterate through a UL/LI. This works perfectly, my problem is returning the value to another variable. What's the best method for this? Thanks!

Collapsible.accordionMobile= function(){
    var distance;
    $(window).scroll(function(event) {  
        var colapsible = $('.collapsible-action').hasClass('collapsible-opened');
        if(colapsible) {
            $(".tabs").each(function(){
                $(".tab1").click(function(){
                    var clickedEvenAccordion = $(this).offset().top;
                    //console.log('clickedEvenAccordion: ' + clickedEvenAccordion );
                    var scrollTop     = $(window).scrollTop(),
                        elementOffset = clickedEvenAccordion;
                        distance      = (elementOffset - scrollTop);
                    console.log('distanceswq' + distance);
                    var windowScrolledTopValue = $(window).scrollTop();
                    console.log('windowScrolledTopValue' + windowScrolledTopValue);

                });
                return false;
            });

        }
    });
return distance;
}
jinfy
  • 147
  • 1
  • 12
  • you can't, `.scroll` is asyncronous – Ayush Gupta Feb 10 '18 at 14:42
  • You can't, the value of `distance` is assigned a way later after your code has been executed. See https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Teemu Feb 10 '18 at 14:43
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Ayush Gupta Feb 10 '18 at 14:44
  • You can't use a value that hasn't been set. – David Feb 10 '18 at 14:44
  • Fine, Value is not returning, even i removed $(window).scroll. – jinfy Feb 10 '18 at 15:58

0 Answers0