$.getJSON is asynchronous, obviosly.

This commit is contained in:
Serge Victor 2016-09-21 12:45:50 +00:00
parent f2820944f1
commit 90fe6292ca
1 changed files with 18 additions and 15 deletions

View File

@ -34,13 +34,13 @@ if (id) {
$("<p />").text(data.amount/100000000 + "BTC").appendTo($("p#amount"));
$("a").attr("href", data.URI);
$("<p />").text("Powered by Electrum").appendTo($("p#powered"));
var websocket_server = data.websocket_server;
var websocket_port = data.websocket_port;
$(function () {
var current;
var max = 100;
var initial = data.time;
var duration = data.exp;
var websocket_server = data.websocket_server;
var websocket_port = data.websocket_port;
if(duration){
var current = 100 * (Math.floor(Date.now()/1000) - initial)/duration;
$("#progressbar").progressbar({
@ -59,6 +59,22 @@ if (id) {
var interval = setInterval(update, 1000);
}
});
var wss_address = "wss://" + websocket_server + ":" + websocket_port +"/");
console.log("Opening WSS: " + wss_address)
var ws = new WebSocket(wss_address);
ws.onopen = function() {
ws.send('id:' + id);
};
ws.onmessage = function (evt) {
var received_msg = evt.data;
if(received_msg == 'paid'){
$("#container").html("This invoice has been paid.");
}
else alert("Message is received:"+ received_msg);
};
})
.fail(function() {
console.log("error fail");
@ -66,19 +82,6 @@ if (id) {
});
};
var ws = new WebSocket("wss://" + websocket_server + ":" + websocket_port +"/");
ws.onopen = function() {
ws.send('id:' + id);
};
ws.onmessage = function (evt) {
var received_msg = evt.data;
if(received_msg == 'paid'){
$("#container").html("This invoice has been paid.");
}
else alert("Message is received:"+ received_msg);
};
// See http://stackoverflow.com/questions/29186154/chrome-clicking-mailto-links-closes-websocket-connection
$(document).on('click', 'a[href^="bitcoin:"]', function (e) {
e.preventDefault();