aboutsummaryrefslogtreecommitdiffstats
path: root/js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2012-10-28 20:48:24 -0700
committerAdam Soltys <[email protected]>2012-10-28 20:48:24 -0700
commit2af62c8a50444359408a88cb5b2ab3a4f813fad8 (patch)
tree80dcf811cbe5160a91113d707bbcc4c419b8e0d4 /js
parentf96887f4ba736f83c6a994f192e6d555a679b44a (diff)
downloadcoinos-server-2af62c8a50444359408a88cb5b2ab3a4f813fad8.tar.gz
coinos-server-2af62c8a50444359408a88cb5b2ab3a4f813fad8.zip
Gracefully handle connection failures
Diffstat (limited to 'js')
-rw-r--r--js/bitcoin_pos.js46
1 files changed, 36 insertions, 10 deletions
diff --git a/js/bitcoin_pos.js b/js/bitcoin_pos.js
index adbd5fbf..2416ec6b 100644
--- a/js/bitcoin_pos.js
+++ b/js/bitcoin_pos.js
@@ -2,12 +2,16 @@ $(function() {
var websocket = null;
var title = getParameterByName('title');
var address = getParameterByName('address');
+ var commission = getParameterByName('commission');
var logo = getParameterByName('logo');
var exchange = 0;
if (title != "")
$('#title').html(title);
+ if (commission == "")
+ commission = 3;
+
if (logo != "") {
$('#logo').attr('src', logo).show();
$('#title').hide();
@@ -19,16 +23,7 @@ $(function() {
$('#received').hide()
setupQR();
- setInterval(setupSocket, 5000);
-
- $.getJSON('ticker.json', function (data) {
- exchange = 1000 / data.out;
- exchange = exchange + exchange * 0.03;
- exchange = Math.ceil(exchange * 100) / 100;
- $('#exchange').val(exchange.toFixed(2));
- updateTotal();
- $('div.container-fluid').fadeIn('slow');
- });
+ setupSocket();
$('#amount').keyup(updateTotal);
$('#amount').focus();
@@ -39,6 +34,10 @@ $(function() {
updateTotal();
});
+ $('#error').click(function() {
+ location.reload();
+ });
+
function updateTotal() {
var amount = parseFloat($('#amount').val());
var total = amount / exchange;
@@ -52,14 +51,41 @@ $(function() {
displayQR('bitcoin:' + address + '?amount=' + total.toString());
}
+ function fetchExchangeRate() {
+ $.getJSON('ticker.php', function (data) {
+ if (data == null) {
+ $('#error').show();
+ } else {
+ exchange = 1000 / data.out;
+ exchange = exchange - exchange * commission * 0.01;
+ exchange = Math.ceil(exchange * 100) / 100;
+ $('#exchange').val(exchange.toFixed(2));
+ updateTotal();
+
+ $('#error').hide();
+ $('#calculator').fadeIn('slow');
+ }
+ });
+
+ setTimeout(fetchExchangeRate, 900000);
+ }
+
function setupSocket() {
+ setTimeout(setupSocket, 10000);
+
if (!websocket || websocket.readystate != 1) {
websocket = new WebSocket("ws://api.blockchain.info:8335/inv");
websocket.onopen = function() {
websocket.send('{"op":"addr_sub", "addr":"' + address + '"}');
+ fetchExchangeRate();
}
+ websocket.onerror = websocket.onclose = function() {
+ $('#calculator').hide();
+ $('#error').show();
+ };
+
websocket.onmessage = function(e) {
var results = eval('(' + e.data + ')');
var from_address = '';