title: I am i am using setInterval to record time. and i clear the interval Using clearInterval but not working
body: <p>I am using Youtube api, here i need to record the user watch time, so i use setInterval function to reord the time. when video play record function runs after every 500 ms sec using setInterval. and when video pause i clear that id (i.e return by sertInterval) using clearInterval, but is not working. record function still running. and in the meanwhile i start video again at that point the record function run more faster than given interval(500). calling 4-6 time in 1 sec.Is am i doing anything wrong . please suggest me. here below is my code:</p>
  
  <pre><code>function onPlayerStateChange(event){
      if(event.data === YT.PlayerState.PLAYING || event.data === YT.PlayerState.UNSTARTED){
          if(!timeSpent.length){
              for(var i=0, l=parseInt(player.getDuration()); i&lt;l; i++) 
              timeSpent.push(false);
          }
          timer = setInterval(recordTime, 500);
  
      }else if(event.data === YT.PlayerState.PAUSED){ // paused or move forward
          if(parseInt(event.target.getCurrentTime()) == parseInt(lastSeenPoint)){ // paused
              clearInterval(timer);
              timer = 0;
          }else if(event.target.getCurrentTime() &gt; lastSeenPoint || 
              event.target.getCurrentTime() &lt; lastSeenPoint){ // move forward
              seekcount = seekcount+1;
          }
  
      }else if(event.data === YT.PlayerState.BUFFERING){
          clearInterval(timer);
      }else if(event.data === YT.PlayerState.ENDED){ // video completed           
          lastSeenPoint = 0;
          setVideoTrackData();
          isVideoEnd = true;
          clearInterval(timer);
      }
  }
  
  
  function recordTime(){
      var recordExist = false;    
      lastSeenPoint = player.getCurrentTime();   
      if(!isVideoEnd){
          setVideoTrackData();
      }
      timeSpent[ parseInt(player.getCurrentTime()) ] = true;
      calculateActualTime();
      var currentInterActivity = _.filter(interactivity, function(item){return item.tick == parseInt(player.getCurrentTime()) });
      console.log("interactivitytaken : "+isInteractivityTaken);
      if(currentInterActivity.length &amp;&amp; currentInterActivity[0].tick==parseInt(player.getCurrentTime()) &amp;&amp; isInteractivityTaken == false){
          player.pauseVideo();
          isInteractivityTaken = true;
          showInteractivityModal(currentInterActivity);
      }else{      
          isInteractivityTaken = false;
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Vf4Vg.png" rel="nofollow noreferrer">enter image description here</a></p>