在javascript中禁用超时,其中已定义超时

时间:2013-10-31 10:17:47

标签: javascript jquery timeout

我的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");});

        }
});
}

干杯

2 个答案:

答案 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");});

        }
});