after successful ajax call I have jquery open toast function:
function successMessage() {
$("#toast_success").show("slow").delay(3000).hide("slow");
}
Some people might want to close that toast before 3s [ delay(3000) ]:
$(".toast_close").on('click', function() {
$("#toast_success").hide("slow");
});
Unfortunately on click event do not work, message dissapears after 3seconds, and thats it. Any suggestions how to make work "$(".toast_close").on('click', function()"
?
2
Answers
The issue is because the
delay()
call is still on the animation queue. Thehide()
method, when called with a time argument, will also use the animation queue. As such, thehide()
is blocked untildelay()
ends.To fix this, either use
hide()
with no argument, although this may be jarring in your UI, or callstop(true)
to clear the animation queue before you callhide()
:The .delay() method allows us to delay the execution of functions that follow it in the queue. SO we need to stop the animation queue. For Stoping the animation we use stop(true) or stop() method and then hide slowly.