清除超时的问题
Problems with clearTimeout
当我的一个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);
请参阅文档
这是因为代码的执行周期,一旦函数开始执行,它将运行,直到您从该函数返回。 您能否告诉您要实现的功能可能有助于理解,并且可以想到不同的方法
相关文章:
- iOS Safari Javascript设置超时问题
- Selebyun爬网程序超时问题C#
- 是否从超时内开始间隔是一个问题
- 设置超时功能在 chrome 扩展程序弹出窗口.js问题
- 清除超时的问题
- 加载资源时出现 OpenTok JS 连接超时问题
- 设置超时问题
- 线程休眠和设置超时问题
- 超时时单击动态标签时出现问题
- 正在获取ice候选项收集的超时问题
- 超时和滚动问题
- 会话超时问题
- 在For循环上设置超时/延迟的问题
- 下拉菜单超时问题
- 会话超时有问题
- JQuery超时问题与mouseout函数
- 这个清除超时有什么问题?
- setTimeout问题,则没有超时
- 多个javascript超时-实时数据获取的问题
- 循环延迟超时问题