I am trying to make a custom pop up message, that appears, displays to the user for 5 seconds and then fades out. This works fine BUT if the use triggers the event multiple times and the time out is already running the message quickly disappears.
My function so far…
function showMessage(message) {
$(".messageText").text(message);
$(".message").fadeIn("slow");
closeBox = function(){
$(".message").fadeOut("slow");
}
clearInterval(closeBox);
setInterval(closeBox, 5000);
}
Many thanks
Try this:
You need to assign the return of
setIntervalto a variable. This handle can be used to end the interval withclearinterval. (You can’t clear a interval by function, only by interval handle)Also, I pulled the
closeBoxfunction out of theshowMessagefunction, it’s not necessary to declare it every timeshowMessageis called.