Here is a suggested solution:
// Formats numbers with commas.
String.prototype.commafy = function()
{
return this.replace(/ (^|[^\w.])(\d{ 4,})/ g, function($0, $1, $2) {
return $1 + $2.replace(/\d(?= (?:\d\d\d) + (? !\d))/ g, "$&,");
});
};
/**
* Rounds decimals.
* @param {number} num - The number to round...
* @param {number} scale - How many decimal places?
*/
function fixed (num, scale) {
return (+(Math.round(+(num + "e" + scale)) + "e" + -scale)).toFixed(scale);
}
// Declare variables
var totalCap;
var totalCapTemp;
var bitcoinPriceTemp;
var bccPriceTemp;
var bitcoinMarketCap;
var bccMarketCap;
var bitcoinMarketCapTemp;
var bccMarketCapTemp;
var bitcoinMarketShareTemp;
var bccMarketShareTemp;
var bitcoinMarketShare;
var bccMarketShare;
var bitcoinVolTemp;
var bccVolTemp;
var bitcoinChangeTemp;
var bccChangeTemp;
var bitcoinTxTemp;
var chart;
var myVar = setInterval(myTimer, 60000);
// When document is ready
$(document).ready(function() {
myTimer();
});
// Function to run in order
function runInOrder(callback)
{
getTotalMarketCap(function() {
getAllOtherValues(function() {
getMarketShare(callback);
});
});
}
// Get total market cap
function getTotalMarketCap(callback)
{
$.getJSON("https://api.coinmarketcap.com/v1/global/", function(json) {
// Processing data
totalCap = fixed (parseFloat(json.total_market_cap_usd / 1000000000), 2);
// Display data on the page
if (!totalCapTemp || totalCapTemp == totalCap)
{
$("#totalCap")
.text("$" + totalCap + "B")
.removeClass();
}
else if (totalCap > totalCapTemp)
{
$("#totalCap")
.text("$" + totalCap + "B")
.removeClass()
.addClass("green");
}
else
{
$("#totalCap")
.text("$" + totalCap + "B")
.removeClass()
.addClass("red");
}
totalCapTemp = totalCap;
callback();
});
}
// Get all other values
function getAllOtherValues(callback)
{
// Process data
var lastInstruction = function(){
// Get data from API
$.getJSON("https://api.blockchain.info/stats?cors=true", function(json) {
// Process Bitcoin transactions
bitcoinTx = json.n_tx.toLocaleString();
// Display Bitcoin transactions
if (!bitcoinTxTemp || bitcoinTxTemp == bitcoinTx)
{
$("#bitcoinTx")
.text(bitcoinTx)
.removeClass();
}
else if (parseInt(bitcoinTx) > parseInt(bitcoinTxTemp))
{
$("#bitcoinTx")
.text(bitcoinTx)
.removeClass()
.addClass("green");
}
else
{
$("#bitcoinTx")
.text(bitcoinTx)
.removeClass()
.addClass("red");
}
bitcoinTxTemp = bitcoinTx;
callback();
});
};
// Get data from API
$.getJSON("https://api.coinmarketcap.com/v1/ticker/?limit=10", function(
json
) {
// Process data for Bitcoin and Bitcoin Cash
// Display data on the page
$.each(json, function(index, value) {
if (value.id == "bitcoin")
{
// Bitcoin processing
}
if (value.id == "bitcoin-cash")
{
// Bitcoin Cash processing
}
if (index >= json.length - 1)
lastInstruction();
});
});
}
// Get market share
function getMarketShare(callback)
{
// Calculate Bitcoin market share
// Display Bitcoin market share on the page
// Update chart if needed
// Calculate Bitcoin Cash market share
// Display Bitcoin Cash market share on the page
// Update chart if needed
callback && callback();
}
// Create chart
function createChart()
{
$(function() {
Chart.defaults.global.legend.display = false;
Chart.defaults.global.animation.duration = 0;
var ctx = document.getElementById("myChart").getContext("2d");
$("#myChart").css("background-color", "#222428");
// Create a doughnut chart
chart = new Chart(ctx, {
type: "doughnut",
data: {
labels: ["Bitcoin", "Bitcoin Cash", "Others"],
datasets: [
{
label: "dataset",
backgroundColor: ["#2196f3", "#f44336", "#FFFFFF"],
borderColor: "#000000",
data: [
bitcoinMarketShare,
bccMarketShare,
fixed (100 - bccMarketShare - bitcoinMarketShare, 2)
]
}
]
},
options:
{
cutoutPercentage: 70,
rotation: 185
}
});
});
}
Make sure to call the callback inside $.getJSON's success handler