如何打印calAmount和文本?

时间:2019-01-23 10:07:38

标签: javascript websocket javascript-objects

我想使用websocket跟踪比特币地址。我有一个文本框和一个按钮,用户输入比特币地址并单击按钮。我想显示旧交易。我该怎么办?

html代码

<form id="frm1" >
    <input type="text" name="tracking"><br>
</form>
<button onclick="myFunction()">Try it</button>
<p id="demo">Network is ... </p>
<p id="websocket">monitoring transactions</p>

javascript代码

function myFunction() {
    var btcs = new WebSocket("wss://ws.blockchain.info/inv");
    btcs.onopen = function() {
        btcs.send(JSON.stringify({"op":"addr_sub", "addr":text}));
    };
    btcs.onmessage = function (onmsg) {
        var response = JSON.parse(onmsg.data);
        var getOutputs = response.x.out;
        var countOuts = getOutputs.length;
        for (i=0; i < countOuts; i++){
            var outAdd = response.x.out[i].addr;
            if (outAdd == text){
                var amount =response.x.out[i].value;
                var calAmount = amount / 100000000;
                document.getElementById("websocket").innerHTML = "Recieved" + calAmount + "BTC";
            }
        }
        console.log(calAmount);
        console.log(text);
    };
}

已接收100 BTC 接收到0.23 BTC

0 个答案:

没有答案