我编写了一段 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);
关于javascript - jQuery Yii 更新数据表,仅当选项卡显示时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19311982/