0

I'm fetching the current Facebook like count with a code like this:

$(document).ready(function() {
    $.getJSON('https://graph.facebook.com/<username>?callback=?', function(data) {
        var fb_count = data['likes'].toString();
        $('#fb_count').html(fb_count);
    });
});

However, when user browses pages the request is sent again. Is there a way to fetch the data only once per session and keep the fetched value in the HTML element without a new request, until user closes the browser?

Thanks for any help!

user1428033
  • 221
  • 3
  • 12

3 Answers3

2

Yes, you can do it by using HTML5 localstorage,

Below code may help you,

$(document).ready(function() {
    if(typeof(Storage)!=="undefined" && localStorage.fb_count)
    {
        $('#fb_count').html(localStorage.fb_count);
    }
    else{
        $.getJSON('https://graph.facebook.com/<username>?callback=?', function(data) {
           var fb_count = data['likes'].toString();
           $('#fb_count').html(fb_count);
           if(typeof(Storage)!=="undefined")
              localStorage.fb_count=fb_count;
        });
    }
});

Read http://www.w3schools.com/html/html5_webstorage.asp

And http://www.w3.org/TR/webstorage/

Rohan Kumar
  • 38,998
  • 11
  • 69
  • 99
1

You could store the count in a cookie and check for the existence of the cookie before requesting it (How do I set/unset cookie with jQuery?)

if( $.cookie("likeCount") === null ){
   $.getJSON('https://graph.facebook.com/<username>?callback=?', function(data) {
      var fb_count = data['likes'].toString();
      $.cookie("likeCount", fb_count);
      $('#fb_count').html(fb_count);
   });
} 
else 
   $('#fb_count').html($.cookie("likeCount"));

[Note: code not tested]

Community
  • 1
  • 1
Scott Mutch
  • 146
  • 1
  • 3
0
var _facebooker = new function() {

    var url = 'https://graph.facebook.com/<username>?callback=?',
        threshold = 3,
        sKey = "_facebooker",
        mode = (window.localStorage ? "storage" : "jcookie");


    function getData(){
       var data = {};

       if (mode === "jcookie")
          data = JSON.pase($.cookie(sKey));
       else 
          data = JSON.parse(localStorage.getItem(sKey));

       if (typeof data.expired\s == "undefined" || data.expires <= new Date())
           return getNewData();
       else
           return data;
    }

    function setData(d){
       if (mode === "jcookie")
          $.cookie(sKey, JSON.stringify(d));
       else 
          localStorage.setItem(sKey, JSON.stringify(d));
       return d;
    }

    function getNewData(){
       var data = {};
       $.getJSON(url, function(d){ 
          data = { 
              likeCount: d['likes'], 
              expires: new Date((new Date()).getTime() + threshold * 60000)
          };
          setData(data);
       });
       return data;
    }

    function getLikeCount(){
        return getData().likeCount;
    }
    return {
       "getLikeCount" : getLikeCount
    }
}



$(document).ready(function() {
     $('#fb_count').html(_facebooker.getLikeCount());
}
Malk
  • 11,107
  • 4
  • 31
  • 32