javascript - 清除超时的问题

标签 javascript jquery settimeout

当我的一个 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);

请参阅documentation

<小时/>

此外,由于超时是一个常量,因此您可以在此处使用函数 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);

参见documentation

关于javascript - 清除超时的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27776482/

相关文章:

javascript - 是否可以将 setTimeout 函数存储在本地存储中?

javascript - setTimeout 测试未按预期运行

javascript - 为 Bootstrap 单选按钮上的单击/更改添加事件监听器

javascript - 如何在Jquery中查找没有类的元素?

javascript - 如何在带有井号的链接中强制页面重新加载?

javascript - 带有接收推送通知的服务 worker 的 chrome 扩展

javascript - 如何取消绑定(bind)特定 div 内的所有事件

javascript - 每 x 秒调用一个 Javascript 函数并在 y 秒后停止?

javascript - 在each循环中选择div

javascript - 如何区分拖动到纸张元素的形状上的双击、右键单击和鼠标滚轮单击 JointJS Raappid