Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

chronosugar

chronosugar lets you deal with time-related calculations in JavaScript with style and depends on no particular JavaScript framework whatsoever.

Units

The baseline unit is milliseconds:

(3).hours() + (14).minutes() == 11640000

If you're not fond of surrounding your numbers with parens, you could also use the decimal point syntax hack:

7..days() + 1..hour() == 608400000

The following unit methods are added to Number.prototype:

  • seconds
  • minutes
  • hours
  • days
  • months
  • years

Their singular equivalents are aliased as expected; 1..minute() works just fine.

Going back

The ago method yields a JavaScript timestamp in the past, whereas the toDate method on a Number returns a Date, assuming said Number is a JavaScript timestamp.

>>> new Date()
Date {Fri Jun 01 2012 20:05:21 GMT-0700 (Pacific Daylight Time)}
>>> 2..minutes().ago().toDate()
Date {Fri Jun 01 2012 20:03:21 GMT-0700 (Pacific Daylight Time)}

Timers

Two more methods are added to Number.prototype:

  • after
  • repeat

So instead of using window.setTimeout and window.setInterval, you can instead write:

3..minutes().after(function() {
  window.location.reload();
}); // Reload the page in 3 minutes

And annoy your user with style:

5..seconds().repeat(function() {
  alert("Hey listen!");
}); // Nag every 5 seconds

Both methods return a Timer object:

var nag = 3..seconds().repeat(function() {
  alert("Set us as your homepage!");
});

5..minutes().later(function() {
  nag.clear();
});

The Timer object contains an id property; don't change this. It's the integer that's needed to call clearTimeout or clearInterval.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages