Requests between Web and PLC Siemens small delay

Asked

Viewed 28 times

1

This Jquery function is controlled when a Switch from my page is made a change

$("#statusM1").change(function () {

        if (statusM1 == 0) {
            url = "index.html";
            name = '"motor"';
            val = 1;
            sdata = escape(name) + '=' + val;
            $.get(url, sdata, function (result) { });
        } else if (statusM1 == 1) {
            url = "index.html";
            name = '"motor"';
            val = 0;
            sdata = escape(name) + '=' + val;
            $.get(url, sdata, function (result) { });
        }
    });


$.ajaxSetup({ cache: false });
    setInterval(function () {
        $.get("IOCounter.html", function (result) {

            statusM1 = result.trim();
            $('#counter').text(statusM1);

            if (statusM1 == 1) {
                $('#statusM1')[0].MaterialSwitch.on();
            } else {
                $('#statusM1')[0].MaterialSwitch.off();
            }

        });
    }, 1000);

setInterval has the functionality to collect a data from the Iocounter.html page

The above script is working but I realize that at the time of change there is a delay a little confusion at the time of change and the request.

$.get(url, sdata, Function (result) { });

Explaining a little better we have the following scenario:

1st Initially the Switch is at the ON position,

2nd then when clicking it goes to the OFF,

3º and with it executes the function . change,

4º we have an interval in which the request is not made then the Switch goes back to the ON, as it has not received the true value of setInterval.

5th after this little mix-up on the Switch takes the correct request amount and goes back to the OFF

What needs to be done to ease this inconvenience ?

  • Do not use setInterval to make requests. Use setTimeout.

  • Try it this way: http://jsfiddle.net/1gL3h7zn/

  • @sam is the following how can happen to turn off or turn on the equipment on the site the page has to update because otherwise it becomes the biggest mess, understood , and another all electricians some 15 will have access so just imagine the mess one turns on the other turns off, understood, o o porque do setInternal

  • @Sam as I am new in Jquery was the first command I found on the internet, if you have dirty will be very welcome.

  • I suggested using setTimeout because requests will be queued, one after the other. Using setInterval the requests will be made in a disorganized way, IE, will make one request after the other without wanting to know if the previous one has been processed. This can crash the browser and even cause the server to suspend access because it thinks there is abuse (if it has this protection) or create a bottleneck. The suggestion I put up just organizes the requests, and the page will be updated in the same way.

  • @Sam worked, so I ask you what difference between setInterval and setTimeout ? this Web system is hosted in a Siemens PLC which is a controller widely used in Industrial Automation.

Show 2 more comments

1 answer

1


As discussed in the comments, use Ajax requests with setInterval is not indicated because it is asynchronous to Ajax processing. The return of Ajax can take milliseconds or even a few seconds, so calling requests with a preset interval can create a bottleneck on the server or lock the browser.

Is indicated to use setTimeout after Ajax processing. This way each request will be made in an orderly and queued form.

To do this, put Ajax inside a function and call it again after the return:

function f(){
  $.get("IOCounter.html", function (result) {

      statusM1 = result.trim();
      $('#counter').text(statusM1);

      if (statusM1 == 1) {
          $('#statusM1')[0].MaterialSwitch.on();
      } else {
          $('#statusM1')[0].MaterialSwitch.off();
      }
      // chama novamente a função após 1 segundo, criando um loop
      setTimeout(f, 1000);
  });
}

// chama a função após 1 segundo.
setTimeout(f, 1000);

// Ou, se esse segundo inicial não for necessário
// pode chamar a função sem intervalo com

f();

// removendo a linha setTimeout(f, 1000); acima

Remembering that the function of $.get will only be called if Ajax succeeds. If there is an error, the setTimeout will not be called and the loop will stop. So it is better to call the setTimeout in the callback .always to ensure that the function will always be called again:

function f(){
  $.get("IOCounter.html", function (result) {

      statusM1 = result.trim();
      $('#counter').text(statusM1);

      if (statusM1 == 1) {
          $('#statusM1')[0].MaterialSwitch.on();
      } else {
          $('#statusM1')[0].MaterialSwitch.off();
      }
  })
  .always(function(){
      // chama novamente a função após 1 segundo, criando um loop
      setTimeout(f, 1000);
  });
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.