At the click of the button, content is slow to load

1

When you click the button sem fio bus the content will be displayed has a delay of about 10 seconds, sometimes I have to make a 5 clicks to show the information I want. How to take this delay?

index

<!DOCTYPE html>
<html lang="pt-br">

<head>
    <title>TESTE DE FREIOS</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>

    <button id='b1'>botao sem fio truck</button>
    <button id='b2'>botao sem fio bus</button>
    <button id='b3'>botao inicio/reiniciar</button>


    <div id='content'></div>

    <script src='lib/jquery.min.js'> </script>
    <script src='js/plc.js'> </script>
    <script src='js/pageManager.js'> </script>
    <script src='js/sensors.js'> </script>


</body>

</html>

js

const MAX_PAGES_TRUCK = 7;
const MAX_PAGES_BUS = 7;

var edge = false;

var page = {
    index: 0,
    line: '',
    path: ''
};

var pageConfig = {
    "bus": MAX_PAGES_BUS,
    "truck": MAX_PAGES_TRUCK
}

$(document).ready(function () {
    page.path = 'home';
    loadPage(page);
});

$('#b1').click(function () {
    button.truck = 1;
});

$('#b2').click(function () {
    button.bus = 1;
});

$('#b3').click(function () {
    window.location = '/';
});


var checkButtons = setInterval(function () {
    for (var station in button) {
        if (page.index == 0) {
            if (button[station]) {
                page.index = 1;
                page.line = station;
                loadPage(page);
            }
        } else {
            if (button[station] && page.line == station && !edge) {
                edge = true;
                page.index++;
                if (page.index > pageConfig[page.line]) {
                    page.line = '';
                    page.index = 0;
                }
                loadPage(page);
            }
        }
    }
}, 250);


function loadPage(page) {
    if (page.line == '' || !page.line) {
        $("#content").load('pages/home.html', function () {
            console.log('Carregando página inicial');
        });
        return;
    }
    $("#content").load('pages/' + page.line + '/' + page.index + '.html', function () {
        edge = false;
        page.path = page.line + '/' + page.index;
        console.log('Página: ' + page.line + ' ' + page.index + ' carregada');
    });

}

var parser = new DOMParser();
var plcDataText;
var plcDataValues;
var button = {
    "truck": 0,
    "bus": 0,
    "reserva_1": 0,
    "reserva_2": 0,
};
var sensors = [];

console.log('Carregando dados do PLC');

var getValuesInterval = setInterval(getIO, 250);

function getIO() {
    $.get('IO.html', function(data) {    
        plcDataValues = [];
        data = parser.parseFromString(data, 'text/html');
        plcDataText = $('.plc-data', data);
        for (var i = 0; i < plcDataText.length; i++) {        
            plcDataValues.push(plcDataText[i].innerHTML);
        }
        updatePLCValues();    
    });
}

function updatePLCValues() {
    button.truck = parseInt(plcDataValues[0])
    button.bus = parseInt(plcDataValues[1])
    button.reserva_1 = parseInt(plcDataValues[2])
    button.reserva_2 = parseInt(plcDataValues[3]) 
    sensors = [];
    for (var i = 4; i < plcDataValues.length; i++) {
        sensors.push(Math.round( parseFloat(plcDataValues[i]) * 1000) / 1000 );
    }
}


const MAX_SENSORS = 9;

var updateManometerInterval = setInterval( updateManometerValues, 500);

function  updateManometerValues() {
    for (var i=0; i < MAX_SENSORS; i++ ) {
        $('#sensor-' + (i + 1) ).val(sensors[i])
    }    
} 
    
asked by anonymous 20.10.2017 / 17:42

1 answer

0

Gabriel Heming noted very well. I noticed that the placement of the line "$ (document) .ready (function () {" was incorrect.

All code must be entered within this structure. I did a test with JQuery v3.2.1 and there was no delay.

link

    
22.10.2017 / 04:17