0

How to resume SetInterval after clearinterval? my code

$(document).ready(function() {
    setTimeout(function(){ 
        var ao =$('ul').hasClass('slides');
        if(ao==true){
            var detik = -1345; 
            var time = setInterval(function(){
                detik+=0.5;
                $('.slides').attr("style","width: "+oke+"px; transition-duration: 0s; transform: translate3d("+detik+"px, 0px, 0px);");
                    if (detik > 0  ){
                         detik = -1345;
                    }
                }, 1);

            $('.flex-prev, .flex-next').on('click',function(){
                clearInterval(time);
            });            
        }
    },3000);

});
slaesh
  • 16,659
  • 6
  • 50
  • 52
John Pydev
  • 21
  • 3
  • by assigning the result of the same call to setInterval again? – Kris Oct 14 '16 at 09:22
  • 2
    Possible duplicate of [jQuery - How to restart setInterval after killing it off with clearInterval?](http://stackoverflow.com/questions/4986667/jquery-how-to-restart-setinterval-after-killing-it-off-with-clearinterval) – evolutionxbox Oct 14 '16 at 09:24

1 Answers1

0

You can make a function for your setInterval(). you can just call the startInterval() function for restart your timer;

var detik = 0;
$(document).ready(function() {
    setTimeout(function(){ 
        var ao =$('ul').hasClass('slides');
        if(ao==true){
            detik = -1345; 
            startInterval();
            $('.flex-prev, .flex-next').on('click',function(){
                clearInterval(time);
            });            
        }
    },3000);

    function startInterval(){
        time = setInterval(function(){
            detik+=0.5;
            $('.slides').attr("style","width: "+oke+"px; transition-duration: 0s; transform: translate3d("+detik+"px, 0px, 0px);");
            if (detik > 0  ){
                 detik = -1345;
            }
        }, 1);
    }

});
P. Frank
  • 5,691
  • 6
  • 22
  • 50