I have function slideUp() which called with a click of button, click event calls another async function removeSlideUp() which returns a promise. but clicking on the button multiple times calls the removeSlideUp() immediately, I want all the clicks to que and call removeSlideup() one after other only after 'transitionend' event is complete. I am new to javascript and do not know where I am stuck
here is my code
async function slideUp() {
await removeSlideUp();
}
function removeSlideUp() {
return new Promise(function (resolve, reject) {
//console.log(smartAlert.classList.contains("slideup"));
if (smartAlert.classList.contains("slideup") === false) {
smartAlert.classList.add("slideup");
smartAlert.addEventListener(
"transitionend",
function () {
console.log("completed");
smartAlert.classList.remove("slideup");
resolve();
},
{ once: true }
);
} else {
smartAlert.classList.remove("slideup");
resolve();
}
});
}
$('button[name="btn_show"]').click(function () {
slideUp();
});
Note: It needs to be done in pure javascript no jquery or other framework I am unable to que the clicks to execute one by one when removeSlideUp is called it does produce desired results
following code allowed to get promises from transitionend event properly. Code promise return code was taken from resolving a promise with EventListener from second approach for answer by @Volune
//lets start playing
const buttonShow = document.getElementById("btn_show");
buttonShow.addEventListener("click", async () => {
let result;
switch (smartAlert.classList.contains("slideup")) {
case true:
smartAlert.classList.remove("slideup"); //this line triger the transition to happen
result = await transitoinEnd("Show alert is complete");
console.log(result);
break;
default:
smartAlert.classList.add("slideup");
result = await transitoinEnd("Hide alert is complete");
console.log(result);
setTimeout(() => {
smartAlert.classList.remove("slideup");
}, 1000);
result = await transitoinEnd("Show alert is complete");
console.log(result);
}
});
const buttonHide = document.getElementById("btn_hide");
buttonHide.addEventListener("click", async () => {
smartAlert.classList.add("slideup"); //this line triger the transition to happen
const result = await transitoinEnd("Hide alert is complete");
console.log(result);
});
const smartAlert = document.getElementById("smart-alert");
const transitoinEnd = (message) => {
return new Promise((resolve) => {
smartAlert.addEventListener(
"transitionend",
(animationendListener = () => {
smartAlert.removeEventListener(
"transitionend",
animationendListener
);
//call any handler you want here, if needed
resolve(message);
})
);
});
};