In my application, I add a loaderDiv
to avoid all userevents on the page.
The loader appears. If the user force a clickevent
(eg. greenContainer onclick) the event is fired after the loader div disappears.
I've created a short example the show you the problem
js:
function appendOverlay(){
var maskDiv = $('<div id="overlay" class="loadmask" onclick:"return false;"><div class="removeDiv" onclick="sleep(3000)"><span>click to remove overlay after 3 seconds</span></div></div>');
$('body').append(maskDiv);
}
function removeDiv(){
$("#overlay").remove();
}
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
removeDiv();
}
How can I solve this problem?
Solved this problem by using setTimeout(removeDiv(),0)
.
This is a problem with the event loop execution in Javascript.
Propergate the removeDiv()
to the next tick and the userEvent onClick()
is execute first.