electerious / basicLightbox

The lightest lightbox ever made.
https://basiclightbox.electerious.com
MIT License
564 stars 55 forks source link

onClose function I call loops indefinitely resulting in maximum call stack exceeded error #33

Closed adam-jones-net closed 4 years ago

adam-jones-net commented 4 years ago

I have just used the onClose event for the first time and I have an issue with the function I call via the onClose event producing a loop that ends in a maximum call stack exceeded error.

To clarify what I do: I create the popup via a function and that works fine. It checks if a cookie has been set with a value of "shown" and it only actually runs if that isn't found.

var promoPopup="";
function createPopup(){
    checkIfShown=readCookie("show_popup");
    if ( !checkIfShown || checkIfShown==null || checkIfShown!="shown"){
                promoPopup=basicLightbox.create(`
            <div id="promoPopupWrap">content of my popup</div>`, {
            onClose: (promoPopup) => {hidePromoPopup()},
        });
}

Then I have this seperate function that closes it which is what I am calling from the onClose event. I use this function because I need to now update the cookie to stop the popup appearing again

function hidePromoPopup(){
    promoPopup.close();
    setCookie("show_popup","shown","","/");
}
adam-jones-net commented 4 years ago

I've resolved. I realised that the promoPopup.close(); isn't needed in the scenario when the popup closes itself so I was trying to call a close() on a popup instance that no longer existed.

Still I'd recommend maybe that the code is hardened to stop an error on this mistake if the instance can't be found.