我的javascript超时,每10秒设置一次,这会重新加载页面的内容,我已经能够成功停止它,但它会最后一次,即它会超时,我按我的按钮停止它,但它最后一次继续,直到10秒钟结束。
有没有办法在页面上取消此预设超时,所以一旦我点击按钮,它将立即停止超时,无论它是否设置为10秒。
loadTransferListTimer = setTimeout(function(){loadTransferList(reloadTime, txt, state);}, parseInt(reloadTime)*1000);
function enableSelect(value)
{
$.ajax({
url: "ajax_requests/enableSelect.php",
type: "POST",
dataType: "json",
data: {"enabled": value },
context: document.body}).done(function() {
if(value == "true")
{
window.clearTimeout('loadTransferListTimer');
document.getElementById('enable_select').className='big-red-button';
document.getElementById('enable_select').innerHTML='stop selected';
var div = document.createElement("div");
div.setAttribute('id', 'disable_select');
div.setAttribute('class', 'big-red-button');
div.innerHTML = 'Undo Selected';
div.onclick = (function(){enableSelect("false");});
$('.big-button-wrapper').append(div);
}
else{
document.getElementById('enable_select').className='big-select-button';
document.getElementById('enable_select').innerHTML='enable select';
$('#disable_select').remove();
document.getElementById('enable_select').onclick = (function(){enableSelect("true");});
}
});
}
干杯
答案 0 :(得分:1)
要清除它,请使用
if(loadTransferListTimer )
clearTimeout(loadTransferListTimer );
答案 1 :(得分:0)
请尝试将loadTransferListTimer
定义为variable
并在没有单一
引用window.clearTimeout(loadTransferListTimer);
var loadTransferListTimer = setTimeout(function(){loadTransferList(reloadTime, txt, state);}, parseInt(reloadTime)*1000);
function enableSelect(value)
{
$.ajax({
url: "ajax_requests/enableSelect.php",
type: "POST",
dataType: "json",
data: {"enabled": value },
context: document.body}).done(function() {
if(value == "true")
{
window.clearTimeout(loadTransferListTimer);
document.getElementById('enable_select').className='big-red-button';
document.getElementById('enable_select').innerHTML='stop selected';
var div = document.createElement("div");
div.setAttribute('id', 'disable_select');
div.setAttribute('class', 'big-red-button');
div.innerHTML = 'Undo Selected';
div.onclick = (function(){enableSelect("false");});
$('.big-button-wrapper').append(div);
}
else{
document.getElementById('enable_select').className='big-select-button';
document.getElementById('enable_select').innerHTML='enable select';
$('#disable_select').remove();
document.getElementById('enable_select').onclick = (function(){enableSelect("true");});
}
});