2012-11-05 38 views
0

我從數據庫中提取日期。前段時間工作正常,但它不是更新時間jQuery Timeago插件與php

<abbr class="timer timeago modified"><?php echo date('Y-m-d H:i:s', strtotime($row['project_create_date'])); ?></abbr> 

JQuery的

$(function() {  
    /*Time Ago*/ 

    prepareDynamicDates(); 
    var time = ''; 
    $('.timer').each(function(){ 
     var time = $(this).text(); 
     $(this).text(jQuery.timeago(time)); 
    }); 

}); 
+2

我假設你需要使用'setInterval()更新文本' –

回答

1

所有你需要使用一個data參數存儲基準日的第一次。使用text將在第一個實例化時被覆蓋。

<abbr class="timer timeago modified" data-create-date="2012-11-05 14:50:11"></abbr> 

然後你可以使用setInterval根據需要更新文本:

var time = ''; 
$('.timer').each(function() { 
    var $el = $(this); 
    setInterval(function() { 
     var time = $el.data("create-date"); 
     $el.text(jQuery.timeago(time)); 
    }, 1000); // 1000ms = 1 second 
}); 

Example fiddle

注意該每秒更新可能會導致在某些瀏覽器性能不佳。我認爲每30秒鐘刷新1分鐘就足以滿足你的需求。