knowthelist / fhem-tablet-ui

UI builder framework for FHEM
MIT License
148 stars 84 forks source link

Reload image only if its visible #221

Open shadow974 opened 6 years ago

shadow974 commented 6 years ago

The image widget should not refresh images which is not displayed to the user. It just creates a lot of traffic on mobile connections.

knowthelist commented 6 years ago

There idea is quite good, but I don't know if its good to continue counting while the image is not displayed. Have you experienced any overflows? I'd feel better if we could pause/resume the counter ...

choenig commented 6 years ago

A simple solution would be to just set the counter to refresh if the image is not visible.

This fixes the overflow-issue and makes sure the image is reloaded after it gets visible again (if and only if the refresh time has been reached:

setInterval(function () {
    counter++;
    if (counter >= refresh) {
        if (url.match(/_=\d+/)) {
            url = addurlparam(url, '_', new Date().getTime());
        }   
        ftui.log(2, 'Update image widget source. URL=' + url);
        if (elemImg.is(":visible") == true) { 
            elemImg.attr('src', url);
            counter = 0;
        } else {
            counter = refresh;
        }
    }
}, 1000);

An even simpler solution (code wise) would be to skip the complete update if the image is not visible:

setInterval(function () {
    counter++;
    if (counter >= refresh) {
        if (elemImg.is(":visible") == false) { 
            counter = refresh;
            return;
        }
        counter = 0;
        if (url.match(/_=\d+/)) {
            url = addurlparam(url, '_', new Date().getTime());
        }   
        ftui.log(2, 'Update image widget source. URL=' + url);
        elemImg.attr('src', url);
    }
}, 1000);