Javascript 如何从另一个函数中清除js计时器

Javascript 如何从另一个函数中清除js计时器,javascript,jquery,timer,Javascript,Jquery,Timer,这里我需要帮助,我只想停止js中setInterval()启动的计时器 但在另一种情况下,我必须停止这个计时器 请告诉我是否可以使用jquery或js杀死页面中的所有计时器 这是我启动计时器的功能:- function get_question(cat_id){ //count = parseInt(count) + 1; $('#action-options').show(); var user_id = '<?php echo $user->ID; ?&

这里我需要帮助,我只想停止js中setInterval()启动的计时器 但在另一种情况下,我必须停止这个计时器 请告诉我是否可以使用jquery或js杀死页面中的所有计时器

这是我启动计时器的功能:-

function get_question(cat_id){
    //count = parseInt(count) + 1;

    $('#action-options').show();
    var user_id = '<?php echo $user->ID; ?>';
    if(cat_id=='')
    {
        var cat_id = $('#cat_id').val();;
    }
    var roomid = $('#roomid').val();
    /*var round = $('.round').find(h4).html();
    console.log('round : ',round);*/
    var current_round = counter;
    //console.log(current_round);
    $.ajax({
        url : '<?php echo admin_url("admin-ajax.php"); ?>',
        type : 'post',
        data : { action : 'get_question', cat_id : cat_id, roomid : roomid,current_round : current_round , user_id : user_id},
        success : function(res){
        //  console.log('current question :',res);
            $('.round').html("<h4>Round "+counter+"</h4>"); 
            res = JSON.parse(res);
            $('.questions').html('');
        //  console.log('length',res.length);
            for(x=0;x<res.length;x++)  
            {
            //  console.log(res[x]);
                //var parsedjson = JSON.parse(res[x]);
                var parsedjson = res[x];
            //  console.log('data :',parsedjson);
                if(parsedjson['options'])
                { 
                var arr = parsedjson.options.split(","); 

                $('.questions').append('<div class="question_text'+x+'" data-id="'+parsedjson.id+'"><h3>'+parsedjson.questions+'</h3></div><div class="options'+x+'" id="pik"><input type="radio" name="options'+x+'" value="'+arr[0]+'"><label>'+arr[0]+'</label><input type="radio" name="options'+x+'" value="'+arr[1]+'"><label>'+arr[1]+'</label><input type="radio" name="options'+x+'" value="'+arr[2]+'"><label>'+arr[2]+'</label><input type="radio" name="options'+x+'" value="'+arr[3]+'"><label>'+arr[3]+'</label> </div> ');
                }
                else{
                    $('.questions').append('<div class="question_text'+x+'" data-id="'+parsedjson.id+'"><h3>'+parsedjson.questions+'</h3></div><div class="options'+x+'" id="kip"><input type="text" name="answer'+x+'" > </div> ');
                }
            }
            var time_limit =0;
            //var   counter = 1;
                $.ajax({
                url : '<?php echo admin_url("admin-ajax.php");?>',
                type : 'post',
                data : {action : 'get_round_time',counter : counter, cat_id : cat_id},
                success : function(response){
                //  console.log('response',response);
                var round_no = counter;
                    time_limit= response;
                //  console.log(time_limit);
                         start_timer = window.setInterval(function(){
                        var timer = time_limit-1; 
                            time_limit=timer;
                            //limit = timer;
                            if(timer==0)
                            { 
                                $('#action-options').css('pointer-events','');
                                $('#action-options').css('cursor','');
                                $('.questions').html('');
                                $('.round').html('<h4>Round '+counter+'</h4>');
                                check_current_round_answers(round_no);
                                if(counter==3)
                                {

                                    if(timer==0) 
                                    {

                                            $('.timer').html('Please wait for your results !'); 
                                            $('.questions').html(''); 
                                            clearInterval(start_timer);
                                            get_final_results();
                                            return false;
                                    }

                                }
                                //console.log('current_round : ',current_round);
                                counter = counter+1;
                                $('.timer').html('');
                                clearInterval(start_timer);
                                get_question(cat_id);

                            }
                          $('.timer').html('<h4>'+timer+'</h4>');

                        }, 1000);
                }

            });






        }
    });
}
函数获取问题(类别id){
//count=parseInt(count)+1;
$(“#操作选项”).show();
var user_id=“”;
如果(类别id=“”)
{
var cat_id=$('#cat_id').val();;
}
var roomid=$('#roomid').val();
/*var round=$('.round').find(h4.html();
console.log('round:',round)*/
无功电流=计数器;
//控制台日志(当前_轮);
$.ajax({
url:“”,
键入:“post”,
数据:{操作:'get_question',cat_id:cat_id,roomid:roomid,current_round:current_round,user_id:user_id},
成功:功能(res){
//log('当前问题:',res);
$('.round').html(“round”+counter+”);
res=JSON.parse(res);
$('.questions').html('');
//console.log('length',res.length);

对于(x=0;x我花了10秒钟查看您的代码(代码太多了),但您需要的所有代码如下:

var intervalID;

function ticker(){
    console.log("tick");
}

function startTicking(){
    intervalID = setInterval(ticker, 1000);
}

function stopTicking(){
    clearInterval(intervalID);
}
这是一种方法
startTicking
启动间隔,另一种方法
stopTicking
停止(清除)间隔。
intervalID
是两个函数都可以访问的声明变量

从您发布的代码中,
start\u timer
尚未声明,因此将成为全局变量:
window.start\u timer

另一方面,如果您错误地声明了
start\u timer
,则在您试图使用它的地方可能无法访问它。例如,此不起作用():


问题是您在这两个函数中都使用了
start\u timer
。根据代码,我看不到它在任何地方声明

使用全局变量并在两个函数之外声明它

var设置\u定时器;

或者,您也可以在全局使用的情况下使用window对象(不推荐)。但如果使用此选项,请确保不覆盖现有的窗口属性

启动计时器
替换为
窗口。启动计时器

如果您有多个
setInterval
,则创建一个全局数组变量并将每个setInterval存储到其中。只需循环此数组并清除间隔即可

var intervalID;

function ticker(){
    console.log("tick");
}

function startTicking(){
    intervalID = setInterval(ticker, 1000);
}

function stopTicking(){
    clearInterval(intervalID);
}
function ticker(){
    console.log("tick");
}

function initTicker() {
    var intervalID;

    function startTicking(){
        intervalID = setInterval(ticker, 1000);
    }

    startTicking();
}

function stopTicking(){
    clearInterval(intervalID); // Uncaught ReferenceError: intervalID is not defined
}