electerious / basicLightbox

The lightest lightbox ever made.

Home Page:https://basiclightbox.electerious.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

adam-jones-net opened this issue · comments

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","","/");
}

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.