2013-10-11 29 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

相關問題