0

I've a problem with this piece of code. I'm new in js and I don't understand why I only can print the latest pair each time I receive a new message throughout the websocket in the callback function. It prints "ethbtc" every time.

var hashtable = {};
var pairs= ['btcusd', 'btceur', 'eurusd', 'ethusd', 'etheur', 'ethbtc'];
for(var y=0;y<pairs.length;y+=1){
    hashtable[pairs[y]] = {bid:0, ask:0, bidinusd:0, askinusd:0};
}

var pusher = new Pusher('de504dc5763aeef9ff52');

for(var y=0;y<pairs.length;y+=1){
    var ob = pusher.subscribe('order_book');
    var temp = pairs[y];
    ob.bind('data', function (data) {
        for(var i = 0; i < data.bids.length && i<=0; i += 1) {
            console.log(temp);
        }
        for(var i = 0; i < data.asks.length && i<=0; i += 1) {
            console.log(temp);
        }
    });
}
  • https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example I think this question and all of its answers can be helpful here – Chaim Friedman Feb 13 '18 at 21:53
  • That's exactly what I was looking for. –  Feb 14 '18 at 16:56

0 Answers0