简体   繁体   中英

Show 2 condition on JSON object

How to show quote and new quote condition on diffrent div . Please help I want to show 2 condition, my code like this :

function setup() {
    output = document.getElementById("output2");
    var ws = new WebSocket('wss://www.binary.com/websockets/v3?l=RU');

    ws.onopen = function(evt) {
        ws.send(JSON.stringify({ticks:'R_50'}));
    }

    ws.onmessage = function(msg) {
        var quote = JSON.parse(msg.data).tick.quote;
        var newquote = quote.substr(-1);
        log(newquote);//here should be semi-colon
    } 

}

function log(info) {
    //Display information on screen
    var p = document.createElement("p");
    p.innerHTML = info;
    output.appendChild(p);
    console.log(info);
}

 function sendMessage(msg) {
    ws.send(msg);
    log("TICKS");
    ws.close();
}

window.addEventListener("load", setup, false);

Why don't you create twi different div in HTML , and then do

HTML

<div id="quote-div"></div>
<div id="newquote-div"></div>

JavaScript

// Let id of div be an argument of function
function log(info, id) {
            var p = document.getElementById(id);
            p.innerHTML = info;
        }
// and use it like
var quote = JSON.parse(msg.data).tick.quote;
var newquote = quote.substr(-1);
log(quote, "quote-div");
log(newquote, "newquote-div");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM