From 0aab8f33d4933bdd29ff7d3f5dc2e8469579a8e0 Mon Sep 17 00:00:00 2001 From: Joel Larsson Date: Mon, 5 Mar 2012 11:50:14 +0100 Subject: [PATCH 1/2] Add callback function that gets called on each refresh to be able to modify the result. --- jquery.timeago.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) mode change 100644 => 100755 jquery.timeago.js 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; } From 0bd786bd0ad8846698d4df97583724e70dfa6761 Mon Sep 17 00:00:00 2001 From: Joel Larsson Date: Mon, 5 Mar 2012 16:12:43 +0100 Subject: [PATCH 2/2] Add example usage in readme --- README.markdown | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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)