Counter Resets

Discussion in 'JavaScript' started by FPForum, Nov 15, 2016.

  1. #1
    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!
     
    FPForum, Nov 15, 2016 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,821
    Likes Received:
    4,539
    Best Answers:
    123
    Trophy Points:
    665
    #2
    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.
     
    sarahk, Nov 15, 2016 IP
  3. FPForum

    FPForum Notable Member

    Messages:
    4,172
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    225
    Digital Goods:
    2
    #3
    Thanks @sarahk! I'll give that a try and see how it does!
     
    FPForum, Nov 15, 2016 IP