This Jquery function is controlled when a switch on my page is performed 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 ability to collect a data from the IOCounter.html page
The above script is working but I realize that at the time of the change there is a delay a little confusion at the time of the change and the request.
$. get (url, sdata, function (result) {});
Explaining a bit better we have the following scenario:
1st Switch is initially ON,
2nd then clicking it goes OFF,
3rd and with this execute the .change function,
4º we have an interval in which the request is not made then the Switch returns to the ON, as it has not yet received the true value of setInterval.
5th after this little confusion in the Switch takes the correct value of the request and returns to the OFF
What needs to be done to mitigate this inconvenience?