Last active
August 29, 2015 14:25
-
-
Save nfabian13/34db2950fcc7cc6db86f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var BitcoinPrices = []; | |
$(document).ready(function() { | |
var priceIntervalStream = Rx.Observable.interval(1000).flatMap(function (param){ | |
$('#cdDiv').html(param); | |
return getPriceStream(); | |
}); | |
priceIntervalStream.subscribe(function(x){ | |
$('#lbl-price').html(x.data.total.amount); | |
BitcoinPrices.push(x.data.total.amount); | |
if(BitcoinPrices.length >= 5){ | |
displayAvg(); | |
} | |
}); | |
}); | |
function getPriceStream() { | |
return $.getJSONAsObservable("/getcurrentprice") | |
.select(function(d) { return d; }); | |
} | |
function intervalStream() { | |
return Rx.Observable.generateWithRelativeTime( | |
10, //initialstate | |
function(x) { return x >= 0; }, //condition | |
function(x) { return x - 1; }, //iterate | |
function(x) { return x; }, //resultSelector | |
function(x) { return 1000; } //timeSelector | |
); | |
} | |
function displayAvg() | |
{ | |
var sum = 0; | |
var avg = 0; | |
var source = Rx.Observable.fromArray(BitcoinPrices).map(function (value, index, obs) | |
{ | |
//return only the latest 5 array values. | |
if( index > Math.abs(BitcoinPrices.length - 5) ){ | |
return value; | |
} | |
return 0; | |
}); | |
var subscription = source.subscribe( | |
function (x) { | |
sum += parseFloat(x); | |
}); | |
avg = sum / 5; | |
$('#avgValue').html(avg.toFixed(2)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment