javascript - Overlay div doesn't prevent the next event that fired in a specific time window -


In my application, I add a loaderDiv to avoid all userevents on the page.

The loader is displayed if the user forces a clickevent (like a green container onclick), the event is removed after the loader divis disappears.

JS:

  function append overlay () {var maskediv = $ ('& lt)  

; Div id = "overlay" class = "loadmask" onclick: "return false;" & Gt; Div class = "removeDiv" onclick = "sleep (3000)" & gt; & Lt; Span & gt; Click to remove overlay after 3 seconds & lt; / Span & gt; & Lt; / Div & gt; & Lt; / Div & gt; '); $ ('Body') attached (maskDiv). } Function removeDiv () {$ ("# overlay"). Remove (); } Function sleep (milliseconds) {var start = new date () GetTime (); For (var i = 0; i & lt; 1e7; i ++) {if ((new date). GetTime () - start) & gt; Milliseconds {break; }} RemoveDiv (); }

How can I solve this problem?


Resolve this problem setTimeout (removeDiv (), 0) . This is a problem with javascript event loop execution.

Align the removeDiv () on the next tick and the userEvent onClick () is executed first.

Strange 'sleeping' function Do you exist there? Why do not you use internal timeouts:

  function sleep (milliseconds) {setTimeout (function () {removeDiv ();}, milliseconds); }   



Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -