Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created February 22, 2017 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpfiddle/08ef89ace86dfee6d0ab08b7f87890de to your computer and use it in GitHub Desktop.
Save phpfiddle/08ef89ace86dfee6d0ab08b7f87890de to your computer and use it in GitHub Desktop.
[ Posted by Thrawn ] add error handler
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Toggle</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>
<script>
var changeStatus = function(status) {
console.log('Send: GPIO ' + status.gpio + ' => ' + status.checked);
$.ajax({
type: 'POST',
url: 'http://main.xfiddle.com/code_18900011.php',
data: status,
dataType: 'json',
success: function(data, textStatus, jqXHR ) {
console.log('Receive: GPIO ' + data.gpio + ' => ' + data.checked);
var toggle = $('input[data-gpio="' + data.gpio + '"]');
$(toggle).data('send', false);
$(toggle).bootstrapToggle(data.checked == 'true' ? 'on' : 'off');
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
}
$(function() {
$('input[data-toggle="toggle"]').change(function() {
if($(this).data('send')) {
changeStatus({
gpio: $(this).data('gpio'),
checked: $(this).prop('checked')
});
}
$(this).data('send', true);
});
});
</script>
</head>
<body>
<div class="container">
<div class="row">
<h1 class="col-md-12">Bootstrap Toggle</h1>
</div>
<div class="row">
<div class="col-md-12">
<input checked data-toggle="toggle" data-on="GPIO 17" data-off="GPIO 17" data-gpio="17" data-send="true" type="checkbox"/>
<input checked data-toggle="toggle" data-on="GPIO 18" data-off="GPIO 18" data-gpio="18" data-send="true" type="checkbox"/>
</div>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment