diff --git a/README.markdown b/README.markdown index f38038d1..361a3fb8 100644 --- a/README.markdown +++ b/README.markdown @@ -37,6 +37,25 @@ into something like this: As time passes, the timestamps will automatically update. +### Usage with callback on refresh + +If you want to have an callback to modify the specific element on refresh, just pass a function as a callback. + +```html + +``` + **For more usage and examples**: [http://timeago.yarp.com/](http://timeago.yarp.com/) **For different language configurations**: [http://gist.github.com/6251](http://gist.github.com/6251) diff --git a/jquery.timeago.js b/jquery.timeago.js old mode 100644 new mode 100755 index bb20c421..09afef91 --- a/jquery.timeago.js +++ b/jquery.timeago.js @@ -101,22 +101,30 @@ return $t.parse(iso8601); } }); - - $.fn.timeago = function() { + + // callback function param that gets called on each refresh + $.fn.timeago = function(callback) { var self = this; - self.each(refresh); + + self.each(function() { + refresh.call(this, callback); + }); var $s = $t.settings; if ($s.refreshMillis > 0) { - setInterval(function() { self.each(refresh); }, $s.refreshMillis); + setInterval(function() { self.each(function() {refresh.call(this, callback)}); }, $s.refreshMillis); } return self; }; - - function refresh() { + + function refresh(callback) { var data = prepareData(this); if (!isNaN(data.datetime)) { $(this).text(inWords(data.datetime)); + // make a call to the callback function if it exists + if (callback) { + callback.call(this, distance(data.datetime)); + } } return this; }