I have some code which should pull Facebook likes/loves for a post and display the count. It does this fine, but every few seconds it will reset and begin counting over.. Can anyone tell why this code counts, then resets, then counts again, then resets again.. <script> "use strict"; var tc = 251, hc = 221, th = $('.ct'), hh = $('.ch'); var hint, tint; function hil(){ hint = setInterval(function(){ hc += 1; // console.log(hc); hh.text(hc); clearInterval(hint); hil(); }, _.random(1, 15) * 1000); } function tru(){ tint = setInterval(function(){ // console.log(tc); tc += 1; th.text(hc); clearInterval(tint); tru(); }, _.random(1, 15) * 1000); } // tru(); // hil(); setInterval(function(){ $.getJSON('https://graph.facebook.com/v2.8/?ids=XXXX_XXXX&fields=reactions.type(LIKE).limit(0).summary(total_count).as(reactions_like),reactions.type(LOVE).limit(0).summary(total_count).as(reactions_love)&access_token=XXXX',function(res){ th.text(res['XXXX_XXXX'].reactions_love.summary.total_count); hh.text(res['XXXX_XXXX'].reactions_like.summary.total_count); setInterval(function(){ th.text(parseInt(th.text()) + 1); }, _.random(5, 25) * 1000); setInterval(function(){ hh.text(parseInt(hh.text()) + 1); }, _.random(5, 25) * 1000); console.log(res); }); }, 10000); </script> Code (markup): thanks in advance!
You have a call to setInterval(function(){ which means the facebook call will be repeated. Take that out and just called the function once and you'll be fine.