jquery-timeago
jquery-timeago copied to clipboard
Only bind timeago plugin to an element once
Currently, every time an element is passed into the $.fn.timeago() method that element is included in the setInterval, regardless of whether or not it has already been included in a previous interval.
We have found this problematic as we use a event to trigger binding of the timeago plugin, like so (within a Sammy.js app): app.bind('widgetsRendered', function(e) { $('.timestamp').timeago(); });
It would be ideal if only elements that were not already bound to timeago were included in the setInterval.
I didn't realize the new Pull Request system auto-creates a new issue. I've closed the original Issue #25 and copied it's text here.
Sorry for the confusion
+1 for fixing this. Right now the naive way to use this plugin with dynamic content is to just call $('.timeago').timeago() after every time you add more content which is problematic because it causes the existing elements to get multiple setIntervals on them. Needs a fix like this.
https://github.com/rmm5t/jquery-timeago/pull/30/ is related but the plugin should work better without using a jquery plugin like livequery
Wouldn't the solution be to use a global array that stores the IDs resulting from setInterval. Every call to setInterval does a clearInterval on the array of IDs (except for the last one) before appending the array with the new interval ID. This way, additional intervals can be set but will be prevented from accumulating
e.g.
if(id_array.length > 1) {
for(var i=0; i < id_array.length-1; i++) {
clearInterval(id_array[i]);
id_array.splice(i,1);
}
}
id_array.push(setInterval([...]));