JSFiddle - React, Tailwind, and code Playground
HTML
Всего: <span class="total"></span><br /><br />
VK: <span class="vk"></span><br />
FB: <span class="fb"></span><br />
TW: <span class="tw"></span><br />
JavaScript
$(document).ready(function() {
// Переменные
var url = 'http://google.com';
var vk = $('span.vk');
var fb = $('span.fb');
var tw = $('span.tw');
// Создаём массив
var total = new Array();
function One() {
var dfd = new $.Deferred();
// Функция округления цифр
function roundCount(number) {
if (number > 999 && number <= 999999) var result = number/1000 + 'k';
else if (number > 999999) var result = '>1M';
else var result = number;
return result;
};
// Вызываем каунтер ВК
$.getJSON('https://vk.com/share.php?act=count&index=1&url=' + encodeURIComponent(url) + '&callback=?', function(response) {});
VK = {};
VK.Share = {};
VK.Share.count = function(index, count) {
// Запись в массив до округления, например:
total.push(count);
// Округляем
vk.text(roundCount(count));
// Выполняем обещание
dfd.resolve();
};
// Вызываем каунтер ФБ
$.getJSON('http://graph.facebook.com/?id=' + encodeURIComponent(url) + '&callback=?', function(response) {
// Запись в массив до округления, например:
total.push(response.shares);
// Округляем
fb.text(roundCount(response.shares));
// Выполняем обещание
dfd.resolve();
});
// Вызываем каунтер ТВИ
$.getJSON('http://cdn.api.twitter.com/1/urls/count.json?url=' + encodeURIComponent(url) + '&callback=?', function(response) {
// Запись в массив до округления, например:
total.push(response.count);
// Округляем
tw.text(roundCount(response.count));
// Выполняем обещание
dfd.resolve();
});
return dfd.promise();
};
function Two() {
...