There are times when my widget needs to process millions of data in mouse leave event. This may take seconds depending upon the records to process. So I have decided to show a waiting popup before processing.
The problem is waiting popup (div overlay) is not visible at all. If I do the processing in a setTimeout with 10+ milliseconds delay, the overlay is visible and works as expected.
Overlay div
<div style="background: black; opacity: 0.6; position: absolute; z-index: 1000; pointer-events: none; width: 810px; height: 450px; left: 8px; top: 29px; display: none;"></div>
In mouse leave event,
overlay.style.display = "block";
widget.processRecords(data); //this function may take seconds depending upon data
overlay.style.display = "none";
I know that absolute positioning should trigger minimal reflow. But it is not working. I thought maybe there is something wrong with my div element. But running the above code with a delay of 10 milliseconds, I can see the overlay till the widget completes processing
Overlay with delay
overlay.style.display = "block";
window.setTimeout(function(){
this.processRecords(data); //this function may take seconds depending upon data
overlay.style.display = "none";
}, 10);
Adding a delay works, but 10 milliseconds of processing gets wasted. Is there any other way to force the browser to reflow an absolutely positioned element. I saw other stack overflow questions about forcing reflows but none of them has a solid answer. I also referred the following link to force reflow of an element https://gist.github.com/paulirish/5d52fb081b3570c81e3a but none of them has any effect other than delay.
Browser: Chrome, Version: 53
Edit: I asked this question to understand more about reflows in browser and how to control them. There are many drawbacks in solution to the other question.
1) Data processing should wait for 10 milli-seconds unnecessarily
2) If it is a simple function, I can use it inside a setTimeout or setInterval easily. But I'm working on a widget which deals with KB's of code. It requires a lot of change addition to the delay.
3)If I need to change the style of 20 div elements, then 200 milliseconds will be wasted
4)The solution in other question uses a timeout inside a timeout and so on for processing.
I'm just saying that the answer in other question is not suitable in this scenario. There should be some other way or method to trigger the reflow of browser using JavaScript and I'm looking for that.