簡體   English   中英

一次只允許運行一個間隔

[英]Allow only one interval to run at a time

我正在制作一個不斷(每3秒)檢查數據庫並將結果使用jQuery.ajax()編寫為HTML表格的應用程序。 我還有一個導航欄,您可以在其中選擇要在表格上加載哪些信息,如下所示:

<div class="row">
        <div class="col-xs-3">
            <button type="button" class="btn" id="showPlaces">Mesas</button>
        </div>
        <div class="col-xs-3">
            <button type="button" class="btn" id="showReservations">Reservaciones</button>
        </div>
</div>
<div class="row">
        <div class="col-lg-12">
            <table class="table table-striped" id="result_table">

            </table>
            </div>
        </div>
</div>

我得到了使其工作的腳本:

var interval;

function showPlaces(){
    $.ajax({
        type:'GET',
        url: 'places.php',
        dataType: 'html',
        success: function(result){
            $('#result_table').html(result);
        } // End of success function of ajax form
    }); // End of ajax call    
}

function showReservations(){
    $.ajax({
        type:'GET',
        url: 'res.php',
        dataType: 'html',
        success: function(result){
            $('#result_table').html(result);
        } // End of success function of ajax form
    }); // End of ajax call   
}

function stopUpdate(){
    clearInterval(interval);
}

$( document ).ready(function() {
    $('#showPlaces').click(function(){
        stopUpdate(interval);
        interval = setInterval(function(){ showPlaces()} , 3000);
    });

    $('#showReservations').click(function(){
        stopUpdate(interval);
        inverval = setInterval(function() { showReservations()}, 3000); 
    });
});

當我運行此命令時,我沒有收到任何錯誤,一旦按下其中一個按鈕,間隔就會無限重復(這是我想要的),如果同時按下它們,它們就會每三秒鍾開始相互重寫一次(我不想要!),按下任一按鈕后,stopUpdate函數會停止執行間隔嗎? 答案可能很明顯,但我對異步網站還是很陌生的

您在這里有錯別字:

inverval = setInterval(function(){showReservations()},3000);

間隔而不是間隔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM