0

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

        }
});
}

干杯

4

2 回答 2

1

要清除它,请使用

if(loadTransferListTimer )
clearTimeout(loadTransferListTimer );
于 2013-10-31T10:21:07.983 回答
0

请尝试将其定义loadTransferListTimer为 avariable并在没有单一的情况下将其清除

引用喜欢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");});

        }
});
于 2013-10-31T10:25:46.630 回答