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. UsesetTimeout
.– Sam
Try it this way: http://jsfiddle.net/1gL3h7zn/
– Sam
@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
– Cyberlacs
@Sam as I am new in Jquery was the first command I found on the internet, if you have dirty will be very welcome.
– Cyberlacs
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
@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.
– Cyberlacs
Let’s go continue this discussion in chat.
– Sam