How to stop running countdown in jquery?

I have a countdown, and when I click a button, I want to stop this countdown. But it does not work. This is my code.

$(document).ready(function (){
var settimmer =1000;
var $dash_url="<?php echo DASH_URL;?>";
$(function (){
            var $interval=setInterval(function() {
                var timeCounter = settimmer;
                var updateTime = eval(timeCounter)- eval(1);
                if(updateTime => 0){
                    settimmer=updateTime;
                    $('.redirect').css({"color":"#FFF"});
                    $(".redirect").html('Redirecting You in '+settimmer+' Second');
                }
                if(updateTime <= 0){
                    clearInterval($interval);
                    window.location.replace($dash_url);
                    return false;
                }
            },1000);

    });
    $('.stop_timer').click(function (){
        clearInterval($interval);
    });

});

+4
source share
1 answer

This will put your $ interval variable in the desired area.

$(document).ready(function (){
var settimmer =1000;
var $dash_url="<?php echo DASH_URL;?>";
$(function (){
            var $interval=setInterval(function() {
                var timeCounter = settimmer;
                var updateTime = eval(timeCounter)- eval(1);
                if(updateTime => 0){
                    settimmer=updateTime;
                    $('.redirect').css({"color":"#FFF"});
                    $(".redirect").html('Redirecting You in '+settimmer+' Second');
                }
                if(updateTime <= 0){
                    clearInterval($interval);
                    window.location.replace($dash_url);
                    return false;
                }
            },1000);
            $('.stop_timer').click(function (){
                clearInterval($interval);
            });
    });
});
+2
source

Source: https://habr.com/ru/post/1614029/


All Articles