当我的一个 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);
关于javascript - 清除超时的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27776482/