ocadotechnology / hexagonjs

A modular, themable collection of components for modern browsers
https://www.hexagonjs.io
Apache License 2.0
51 stars 15 forks source link

Resize Events: error thrown in inactive tabs #392

Open charlieTheBotDev opened 7 years ago

charlieTheBotDev commented 7 years ago

Context

When using components with resize events (most notably DataTable) an error is thrown if you navigate away from the tab and have some form of 'interval' to reload the data (i.e. re-render the component):

Uncaught TypeError: Cannot read property 'firstElementChild' of undefined
    at resetTriggers (hexagon-1.9.0.js:7213)
    at HTMLDivElement.scrollListener (hexagon-1.9.0.js:7229)

This also appears to affect Plot so

Possible Fix

Prevent the resize event from firing (or attempting to fire) when the window is inactive

e.g. using something like:

(function() {
    var time = 10000,
        delta = 100,
        tid;

    tid = setInterval(function() {
        if ( window.blurred ) { return; }    
        time -= delta;
        if ( time <= 0 ) {
            clearInterval(tid);
            myFunction(); // time passed - do your work
        }
    }, delta);
})();

window.onblur = function() { window.blurred = true; };
window.onfocus = function() { window.blurred = false; };

Steps to Reproduce

  1. Create a data table that refreshes using setTimeout or setInterval
  2. Navigate to another tab/window and leave the table running in the background
  3. Return to the page and look at the console, it will be full of errors
charlieTheBotDev commented 6 years ago

We should see if we can resolve this issue when looking at #458