Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature suggestion: Add callback on refresh to be able to modify element #60

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.markdown
Expand Up @@ -37,6 +37,25 @@ into something like this:


As time passes, the timestamps will automatically update. 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
<script type="text/javascript">
jQuery(document).ready(function() {
$("abbr.timeago").timeago(function(distance) {
// my callback function where 'this' is the element and the parameter 'distance' is the difference between the current time and the elements time
var minutes = (distance / 60) / 1000; // get distance in minutes
// add old class if the time passes 10 minutes
if (minutes > 10) {
$(this).addClass("old");
}
});
});
</script>
```

**For more usage and examples**: [http://timeago.yarp.com/](http://timeago.yarp.com/) **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) **For different language configurations**: [http://gist.github.com/6251](http://gist.github.com/6251)
Expand Down
20 changes: 14 additions & 6 deletions jquery.timeago.js 100644 → 100755
Expand Up @@ -101,22 +101,30 @@
return $t.parse(iso8601); return $t.parse(iso8601);
} }
}); });


$.fn.timeago = function() { // callback function param that gets called on each refresh
$.fn.timeago = function(callback) {
var self = this; var self = this;
self.each(refresh);
self.each(function() {
refresh.call(this, callback);
});


var $s = $t.settings; var $s = $t.settings;
if ($s.refreshMillis > 0) { if ($s.refreshMillis > 0) {
setInterval(function() { self.each(refresh); }, $s.refreshMillis); setInterval(function() { self.each(function() {refresh.call(this, callback)}); }, $s.refreshMillis);
} }
return self; return self;
}; };

function refresh() { function refresh(callback) {
var data = prepareData(this); var data = prepareData(this);
if (!isNaN(data.datetime)) { if (!isNaN(data.datetime)) {
$(this).text(inWords(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; return this;
} }
Expand Down