Event listeners actually begin to run their code before the event is finished, hence we have the ability to preventDefault(). But what if I have to do something only after the event is finished and it's really important not to do that earlier?
I've tried placing the code that needs to be run only after the event is finished after the listener's return statement like a self-invoking function:
$(...).on(event, function(evt){
//do stuff
return (function(){
// do stuff after the event
})();
});
But that wasn't helpful. The only thing that seems to work is using setTimeout
. But it's really not a good idea, because it's not deterministic and under certain conditions the timeout may turn out to be insufficient and the idea fails.
How can I do something only after the event is already finished?