一次只允许一个间隔运行

时间:2015-07-23 17:35:53

标签: javascript php jquery ajax

我正在制作一个应用程序,不断(每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函数是否应该停止执行间隔?答案可能很明显,但我不熟悉异步网站

1 个答案:

答案 0 :(得分:1)

你在这里得到错字:

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

inverval而不是间隔

相关问题