Skip to content

Commit fefdc74

Browse files
Christoph Burgmertherabidbanana
Christoph Burgmer
authored andcommitted
Allow timeago to be used with jquery-livequery:
$('attr.timeago').livequery(function() { $(this).timeago(); }); - Save intervalId to only issue setInterval once - Add selected elements to a global array which we query
1 parent fe019eb commit fefdc74

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

jquery.timeago.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
}
2525
};
2626
var $t = $.timeago;
27+
var intervalId = null;
28+
var elements = [];
2729

2830
$.extend($.timeago, {
2931
settings: {
@@ -102,13 +104,19 @@
102104
}
103105
});
104106

105-
$.fn.timeago = function() {
107+
$.fn.timeago = function(clear) {
106108
var self = this;
107-
self.each(refresh);
109+
self.each(function (i, element) {
110+
if ($.inArray(element, elements) == -1)
111+
elements.push(element);
112+
});
113+
$.each(elements, refresh);
108114

109115
var $s = $t.settings;
110116
if ($s.refreshMillis > 0) {
111-
setInterval(function() { self.each(refresh); }, $s.refreshMillis);
117+
if (intervalId === null) {
118+
intervalId = setInterval(function() { $.each(elements, refresh); }, $s.refreshMillis);
119+
}
112120
}
113121
return self;
114122
};

0 commit comments

Comments
 (0)