How to stop interval when the element stops to exist?
This is my code, I just can't get it to work:
            var AmLoadMoreButton = $('.amscroll-load-button');
            var TopSearch = $('#textSearch');
            var isDisabled = TopSearch.prop('disabled');
            window.setInterval(function(){
                if(AmLoadMoreButton.length){
                    LoadMoreProducts();
                    console.log("Load more still exists");
                } else {
                    console.log("that's all, stop now");
                    StopLoadingProducts();
                }
            }, 1000);
            function LoadMoreProducts(){
                if(!isDisabled){
                    TopSearch.prop( "disabled", true );
                    $('#amasty-shopby-product-list').on("click", ".amscroll-load-button", function(){});
                    $('.amscroll-load-button').trigger('click');
                }
            }
            function StopLoadingProducts(){
                console.log("No more products to load");
                TopSearch.prop( "disabled", false );
                clearInterval();
            }
Is that how you stop the interval? The function for "StopLoadingProducts()" is never called. Nothing logged to console, never mind stopping the interval.
 
    