将响应(社交共享计数)从$ .getJSON推送到数组,并将此数组用于其他函数

时间:2015-09-23 19:09:19

标签: javascript json twitter vk

尝试使用共享计数器为三个社交网络的当前URL创建数组:Twitter,Facebook& VK。我需要得到这个数组的所有元素的总和并显示结果(进入其他函数)。

数组必须是这样的[1, 2, 3]

这是我的代码:

$(document).ready(function() {

  var totalCount = new Array();

  // Twitter counter
  $.getJSON('http://cdn.api.twitter.com/1/urls/count.json?url=' + encodeURIComponent(location.href) + '&callback=?', function(response) {
    totalCount.push(response.count); // try to push response.count to array
  });

  // Facebook counter
  $.getJSON('http://graph.facebook.com/?id=' + encodeURIComponent(location.href), function(response) {
    totalCount.push(response.shares); // try to push response.shares to array
  });

  // VK counter
  $.getJSON('https://vk.com/share.php?act=count&index=1&url=' + encodeURIComponent(location.href) + '&callback=?', function(response) {});
  VK = {};
  VK.Share = {};
  VK.Share.count = function(index, count) { 
    totalCount.push(count); // try to push count to array
  };

  // Show array
  console.log(totalCount);

});

但此代码为空(在Chrome控制台中显示[])。

有什么想法吗?请帮帮我。

0 个答案:

没有答案