Last active
August 29, 2015 14:25
-
-
Save nfabian13/c9edc9f43ebdc7788e91 to your computer and use it in GitHub Desktop.
this is the code to display a bitcoin price every 10 senconds and calculate the average of the last 5 prices retrieved from the HTTP resource.
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 = []; | |
function countdown() { | |
var count = 10; | |
setInterval(function() { | |
count--; | |
//display the countdown timer to UI | |
$('#cdDiv').html(count); | |
if(count == 0) { | |
var request = $.ajax({ | |
type: 'GET', | |
url: '/getcurrentprice' | |
}); | |
request.done(function (response, textStatus, jqXHR){ | |
console.log('amount ' + response.total.amount); | |
var p = response.total.amount; | |
//display the price to UI | |
$('#lbl-price').html(the_price); | |
BitcoinPrices.push(p); | |
if(BitcoinPrices.length >= 5) | |
{ | |
displayAvg(BitcoinPrices); | |
} | |
}); | |
count = 10; | |
} | |
}, 1000); | |
} | |
function displayAvg(arr) | |
{ | |
var sum = 0; | |
for(var i = (arr.length - 1); i >= (arr.length - 5); i--) | |
{ | |
sum += parseFloat(arr[i]); | |
} | |
var avg = sum / 5; | |
$('#avgValue').html(avg.toFixed(2)); | |
} | |
countdown(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment