2013-10-11 28 views
1

我已经写了一个JavaScript代码来更新yii GridView表,每隔10秒后向用户显示正常工作。jQuery Yii更新数据表,仅当显示Tab时

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代码中犯了一个错误吗?

此外,我希望表格停止刷新一旦选项卡关闭。我怎样才能做到这一点?

回答

0

'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); 
+0

感谢萨穆埃尔的解决方案,用于更新我的有关知识“表示”因为我是新到jQuery,JavaScript的东西。 – prattom

相关问题