jQuery Yii 更新数据表,仅当显示 tab 时



我编写了一个JavaScript代码,每向用户显示10秒后更新yii GridView表,工作正常。

timedRefresh($page_refresh_time);
function timedRefresh(timeoutPeriod) {
     setTimeout(function(){refreshGrid()}, timeoutPeriod);
}
function refreshGrid() {
   $.fn.yiiGridView.update("group-grid");
   timedRefresh($page_refresh_time);
}

但是现在我想更改我的代码,以便仅在显示选项卡时才刷新表,而不刷新时间表的其余部分。所以我将我的代码更改为:

timedRefresh($page_refresh_time);
function timedRefresh(timeoutPeriod) {
     setTimeout(function(){refreshGrid()}, timeoutPeriod);
}
function refreshGrid() {
   $('a[href="#dash2"]').on('shown', function(e) {
        $.fn.yiiGridView.update("group-grid");
        timedRefresh($page_refresh_time);
   });
}

其中dash2是选项卡的 ID。现在的问题是,在更新我的代码后,每 10 秒刷新一次表已经达到了工作水平。谁能指导我在上面的JavaScript代码中犯了错误?

我还希望表在选项卡关闭后停止刷新。我怎样才能做到这一点?

'shown'不是

有效的jQuery事件。无论如何,您在此方案中使用 on() 是错误的。

将上面的代码更改为:

function refreshGrid() {
   if($("a[href='#dash2']").is(':visible')) {
        $.fn.yiiGridView.update("group-grid");
   }
}
var interval = setInterval(refreshGrid, $page_refresh_time);

如果需要取消刷新,请使用

if(interval) clearInterval(interval);

最新更新