How to clear interval and set it again?

前端 未结 5 1149
离开以前
离开以前 2020-12-30 03:15

This is what i am trying to accomplish: when the last slide is reached fadeOut last slide and then fadeIn first slide, and then clearInterval (everything works with th

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-30 03:51

    Solution for timer which counts 15 sec then goes to main page, if there is click on screen resets timer to the start. It may be useful to someone. (x is counter for touch events) :

      var productInterval = null;
      var counterForGoToMainPage = 15;
    
      function startTimer(){
        counterForGoToMainPage--;
    
        if(counterForGoToMainPage == 0){
          clearInterval(productInterval);
          counterForGoToMainPage = 15;
          window.location.href = "/";
          }
      }
    
      function countTouches(event) {
      var x = event.touches.length;
    
        if(x == 1) { 
          clearInterval(productInterval);
          counterForGoToMainPage = 15;
          productInterval = setInterval(function(){
             startTimer();
          },1000);
        }
      }
    

提交回复
热议问题