清除超时的问题

Problems with clearTimeout

本文关键字:问题 超时 清除      更新时间:2023-09-26

当我的一个div被点击时,我有以下方法:

   current_money = parseInt($(this).find('.text-white').text());
   var  current_category_id = $(this).find('.category_id').val();
   game.getQuestion(current_category_id, current_money);
   game.selected_question = $(this);
    var result = 15;
    var seconds = 15;
     check = function(){
        if(seconds < 0)
        {
            checkAnswer(-1, 0, null);
        }
        else
        {
            var percentage = Math.round((seconds / result) * 100);
            $('#countDownChart').data('easyPieChart').update(percentage);
            $('#time').text(seconds);
            seconds--;
            setTimeout(check, 1700); // check again in a second
        }
    }
    check();

这将显示一个弹出窗口。

单击弹出窗口中的元素时,它将调用以下方法:

    function checkAnswer(id, is_correct, element)
{
    clearTimeout(check);
    blink(element, is_correct)
    var new_value;
    if(is_correct)
    {
       var old_value =  $('#game_points').text();
       new_value = parseInt(old_value)+current_money;
       game.num_correct++;
    }
    else
    {
        var old_value =  $('#game_points').text();
        new_value = parseInt(old_value)-current_money;
    }
    current_money = new_value;
    game.score = current_money;
    $('#game_points').text(new_value);
    game.disableQuestion();

    $.ajax({
        type: 'POST',
        url: '/Jeopardy/setAnswer',
        dataType: 'json',
        data: {
            request: 'ajax',
            answer_id: id,
            game_id: game.id,
            phase_id: game.phase_id,
            question_id: game.current_question.id,
            is_correct:is_correct
        },
        success: function (data)
        {
        }
    });
    game.questionBox.fadeOutAnimation();
    game.num_questions--;
    if(game.num_questions == 0)
    {
        game.finish();
    }
}

如您所见,第一行我清除了超时。

但是,它继续运行,直到秒数达到 0,然后调用 checkAnswer 方法。

谁能告诉我为什么会这样?

更新

这是我的脚本顶部:

    readUrlVars();
var status = true;
var current_question = null;
var questionBox;
var game = null;
var current_money;
var check;
jQuery(document).ready(function () {
    init();
    $('#game_table').on('click','.question',function()
    {
        if(status == 'true')
        {
           current_money = parseInt($(this).find('.text-white').text());
           var  current_category_id = $(this).find('.category_id').val();
           game.getQuestion(current_category_id, current_money);
           game.selected_question = $(this);
            var result = 15;
            var seconds = 15;
             check = function(){
                if(seconds < 0)
                {
                    checkAnswer(-1, 0, null);
                }
                else
                {
                    var percentage = Math.round((seconds / result) * 100);
                    $('#countDownChart').data('easyPieChart').update(percentage);
                    $('#time').text(seconds);
                    seconds--;
                    setTimeout(check, 1700); // check again in a second
                }
            }
            check();
        }
    })
    $('#option_list').on('mouseenter','.optionOutCss', function()
    {
        $(this).attr('class','optionOverCss');
    })
    $('#option_list').on('mouseleave','.optionOverCss', function()
    {
        $(this).attr('class','optionOutCss');
    })
})

更新 2

我将我的代码更新为以下内容:

           timeout = setTimeout(timer, 1700);
function timer()

{

if(seconds < 0)
{
    checkAnswer(-1, 0, null);
}
else
{
    var percentage = Math.round((seconds / result) * 100);
    $('#countDownChart').data('easyPieChart').update(percentage);
    $('#time').text(seconds);
    seconds--;
    setTimeout(timer, 1700); // check again in a second
}
}

    clearTimeout(timeout);

然而,这并没有改变任何事情。

您没有清除超时。

设置超时使用时,

 timeout = setTimeout(check, 1700);

清除超时使用时,

 clearTimeout(timeout);

查看文档


也因为你的超时是一个常数,你可以在这里使用setInterval函数并摆脱递归,

current_money = parseInt($(this).find('.text-white').text());
var  current_category_id = $(this).find('.category_id').val();
game.getQuestion(current_category_id, current_money);
game.selected_question = $(this);
var result = 15;
var seconds = 15;
var check = function(){
    if(seconds < 0)
    {
        checkAnswer(-1, 0, null);
    }
    else
    {
        var percentage = Math.round((seconds / result) * 100);
        $('#countDownChart').data('easyPieChart').update(percentage);
        $('#time').text(seconds);
        seconds--;
    }
}
interval = setInterval(check, 1700);

然后清除它,

clearInterval(interval);

请参阅文档

这是因为代码的执行周期,一旦函数开始执行,它将运行,直到您从该函数返回。 您能否告诉您要实现的功能可能有助于理解,并且可以想到不同的方法