npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

smart-time-ago

v0.1.5

Published

a simple lib to update the relative time on your page

Downloads

12

Readme

smart-time-ago

Smart Time Ago is a little jQuery library to update the relative timestamps in your document. (e.g "3 hours ago"). It's originally built for https://pragmatic.ly/. It's inspired by another jQuery plugin http://timeago.yarp.com/ but give more flexibility and more features.

Installation

You can juse use it as a jQuery plugin. If so, you just need copy the lib/timeago.js or src/timeago.coffee to your javascripts lib then load it after jQuery loaded.

Or if you use node, you can install it from npm.

$ npm install -g smart-time-ago

Why Smart?

smart-time-ago will check and update the relative time every 30000 millisecond (30 seconds) in the scope you specify. Latter it will check the newest time in your scope then change the interval to a proper value.

For example, If the newest time in the scope you specify is '2 hours ago'. The smart-time-ago won't check and update the relative time every 30 seconds, because it's unneccessary. It will automaticly make the interval longer to 15 minutes.

Rules:

The newest time less than 44.5 minutes, the interval will set to 0.5 minutes.

The newest time between 44.5, 89.5 minutes, the interval will set to 15 minutes.

The newest time more than 1 hour and a half, the interval will set to 30 minutes.

Usage

By default smart-time-ago will keep watch on the time elements with a class of timeago and a ISO8601 timestamp in datatime attribute:

<time class="timeago" datetime="2012-07-18T07:51:50Z">about 8 hours ago</time>

You can initialize the smart-time-ago in global like:

$().timeago();

It will watch all your relative time elements by only one TimeAgo instance.

Or you can use it in a specify scope like.

<div class="timeLables">
 <time class="timeago" datetime="2012-07-18T07:51:50Z">about 8 hours ago</time>
 <time class="timeago" datetime="2012-07-18T06:51:50Z">about 9 hours ago</time>
</div>

$('.timelables').timeago();

It will create one TimeAgo instance to update the time elements in the div with timeLables class.

However you can also create TimeAgo instance for every time element separately like:

$('.timeago').timeago();

BTW if you need dynamic add the time element to your document without refreshing the page or you want to refresh the timeago manually. You might need call the refresh function to refresh the smart-time-ago like:

$().timeago('refresh');

Configuration

There are some default configuration in smart-time-ago

$.fn.timeago.defaults = {
  selector: 'time.timeago',
  attr: 'datetime',
  dir: 'up',
  suffix: 'ago'
};

The 'time.timeago' is the default selector for smart-time-ago to watch and update.

The 'datetime' is the default attribute to put the ISO8601 absolute time for smart-time-ago to parse.

The 'up' in dir means the elements in your scope is display by time desc. which means if the dir sets to 'up'. smart-time-ago will treat the first element's time as the newest time to adjust the time interval. Oppositely if it set to 'down', smart-time-ago will treat the last element's time as the newewst time.

The 'ago' in 'suffix' means the relative generated by smart-time-ago will look like '3 hours ago'. If you want the text looks like '3 hours from now', you might need change the 'suffix' to 'from now'.

You can change the default configurations by passing the options to timeago function when initialize timeago like:

$().timeago({selector: 'span.timeago', attr: 'title', dir: 'down', suffix: 'from now'})

Credits

pragmatic.ly

smart-time-ago is maintained and funded by Pragmatic.ly.

Thanks to all the contributors.

Copyright (c) 2012 Terry Tai, Pragmatic.ly ([email protected], https://pragmatic.ly/)