Added $.timeago helper function

This commit is contained in:
Ryan McGeary 2008-07-20 17:08:34 -04:00
parent 0000974025
commit 1a3b36cb18
2 changed files with 22 additions and 6 deletions

View file

@ -1,5 +1,5 @@
/*
* Time Ago (for jQuery) version: 0.2.99 (07/19/2008)
* Time Ago (for jQuery) version: 0.3 (07/20/2008)
* @requires jQuery v1.2 or later
*
* Timeago is a jQuery plugin that makes it easy to support automatically
@ -15,9 +15,9 @@
*/
(function($) {
$.timeago = function(timestamp) {
// TODO: should take a Date, ISO8601, or element[title=iso8601]
// return words if date or iso8601; convert and return element if element
alert("jQuery.timeago helper not implemented yet");
if (timestamp instanceof Date) return inWords(timestamp);
else if (typeof timestamp == "string") return inWords($.timeago.parse(timestamp));
else return $(timestamp).timeago();
};
$.extend($.timeago, {
@ -60,15 +60,21 @@
var $s = $.timeago.settings;
if ($s.refreshMillis > 0) {
setInterval(function() { self.each(refresh); }, ($s.refreshMillis));
setInterval(function() { self.each(refresh); }, $s.refreshMillis);
}
return self;
};
function refresh() {
var date = $.timeago.parse(this.title);
if (!isNaN(date)) {
$(this).text($.timeago.inWords(distance(date)));
$(this).text(inWords(date));
}
return this;
}
function inWords(date) {
return $.timeago.inWords(distance(date));
}
function distance(date) {

View file

@ -23,6 +23,11 @@
var string = $.timeago.inWords(parseInt(this.title) * 1000);
$(this).text(string);
});
// unit tests
$("#helper_date").text($.timeago(new Date()));
$("#helper_string").text($.timeago(iso8601(new Date())));
$.timeago($("#helper_element"));
});
</script>
<style>
@ -83,5 +88,10 @@
<abbr class="towords" title="16416000"></abbr> [190 days]<br />
<abbr class="towords" title="31622400"></abbr> [366 days]<br />
<abbr class="towords" title="94608000"></abbr> [1095 days]<br />
<h1>Helper unit tests:</h1>
You opened this page <abbr id="helper_date"></abbr> [won't refresh]<br />
You opened this page <abbr id="helper_string"></abbr> [won't refresh]<br />
You opened this page <abbr id="helper_element" class="loaded"></abbr> [should refresh]<br />
</body>
</html>