expand ALL or collapse ALL testing

HTML

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<div class="container">
    <!-- <a href="#" class="btn btn-default openall">open all</a> <a href="#" class="btn btn-default closeall">close all</a>--> <a href="#" class="btn btn-default expand-all"> Expand All</a>

    <hr>
    <div class="panel-group" id="accordion">
        <div class="panel panel-default">
            <div class="panel-heading collapsed" data-toggle="collapse" data-parent="#accordion1" href="#collapseOne">
                 <h4 class="panel-title">
          <a class="" >
            Collapsible Group Item #1
          </a>
        </h4>

            </div>
            <div id="collapseOne" class="panel-collapse collapse in">
                <div class="panel-body">ONe</div>
            </div>
        </div>
        <div class="panel panel-default">
            <div class="panel-heading collapsed" data-toggle="collapse" data-parent="#accordion1" href="#collapseTwo">
                 <h4 class="panel-title">
          <a >
            Collapsible Group Item #2
          </a>
        </h4>

            </div>
            <div id="collapseTwo" class="panel-collapse collapse ">
                <div class="panel-body">Two Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.</div>
            </div>
        </div>
      ...

JavaScript

$('.panel-heading').click(function () {
    var me = $(this);
    setTimeout(function () {
        if (me.hasClass('collapsed')) {
            console.log('if hell0');
            $('.btn').removeClass('collapse-all');
            $('.btn').addClass('expand-all ');
            $('.btn').text('Expand All');
        } else {
            var arry = 0;
            setTimeout(function () {
                $('.panel-heading').each(function (index) {

                    var countCollpase = ($('.panel-heading.collapsed').length);
                    console.log(countCollpase);
                    if (!countCollpase == 0) {
                        console.log('im working :)');
                    } else {
                        console.log('im not working :)');
                        $('.btn').addClass('collapse-all');
                        $('.btn').removeClass('expand-all ');
                        $('.btn').text('Collapse All');
                    }
                });

            },10);

        }

        $('.panel-heading').each(function (index) {
            if ($(me).not('collpased')) {
                //console.log(index);
            } else {
                // console.log(index);
            }
        });
    }, 500);

});

function expand() {
    $('.btn').addClass('collapse-all ');
    $('.btn').removeClass('expand-all');
    $('.btn').text('Collpase All');
    console.log('expand');
    $('.panel-collapse').collapse('show');

}

function collapse() {
    $('.btn').removeClass('collapse-all');
    $('.btn').addClass('expand-all ');
    $('.btn').text('Expand All');
    console.log('collpase');
    $('.panel-collapse').collapse('hide');

}

$(document).on('click', '.expand-all', function () {
    expand();
});

$(document).on('click', '.collapse-all', function () {
    collapse();
});