项目作者: nickpalenchar

项目描述 :
A setTimeout/setInterval for Humans (and so much more)
高级语言: TypeScript
项目地址: git://github.com/nickpalenchar/goodtimer.git
创建时间: 2016-07-31T16:36:54Z
项目社区:https://github.com/nickpalenchar/goodtimer

开源协议:MIT License

下载



Snyk Vulnerabilities for GitHub Repo

logo

The Split-Second Precise JavaScript Timer


goodtimer demo

🧐 About

Goodtimer provides an accurate-to-milliseconds way of implementing setTimeout and setInterval. It’s the timer of your
dreams, providing a high-level API to easily manipulate countdowns. Here’s a few things that make Goodtimer so good:

  • It self-corrects delays from the event loop, so it’s guaranteed to stay in sync with time.
  • It comes with a flexible timeExpression syntax, so you can easily express time in a number of desirable ways.
  • Provides drop-in replacement to setInterval.
  • Can be used in projects like react with npm, or directly in the browser via cdn;

Installation & simple usage

Download using npm

  1. npm i --save goodtimer

And use in your code!

  1. const { Timer } = require('goodtimer');
  2. new Timer('1:00');

Or replace your drifty setIntervals with setGoodInterval ⭐️:

  1. const { setGoodInterval } = require('goodtimer').timeutil;
  2. setGoodInterval(() => console.log("exactly 1 second!"), 1000);

💝 Browser-compatible client-side version now available!

  1. <script src="https://cdn.nickpal.to/goodtimer/goodtimer-3.3.0.js"></script>
  2. <script>
  3. new goodtimer.Timer('1:00');
  4. </script>

➡️ Jump into the full docs site here or read below for a few more quick examples :bow:


⏲ Simple Usage

  1. const yourFn = () => {};
  2. new Timer('1:00', yourFn); // replacement for setTimeout
  3. new Timer('1:00', yourFn, { repeat: true }); // replacement for setInterval
  4. const timer = new Timer('5:00'); // (Five minutes)
  5. timer.pause(); // freezes timer at given time
  6. timer.unpause(); // resumes timer
  7. timer.reset(); // resests to initial value (in this case 5 minutes)
  8. timer.toString() // returns in UTC-like format ("5:00.000")
  9. // ~ 1 second later ~
  10. timer.fmtTime("%M minutes %s seconds") // -> "4 minutes 59 seconds" (many ways to use!)
  11. timer.gt('1:00'); // "greater than" -> true
  12. timer.lt('60:00:00'); // "less than (60 hrs)" -> true
  13. timer.equals('6m'); // (6 minutes, alternate notation) -> false
  14. // or use the Time class and skip the controls
  15. const [minute, second] = [new Time('1m'), new Time('1s')];
  16. minute.gt(second) // -> true
  17. second.equals(':01') // -> true
  18. minute.equals(second) // -> false
  19. second.set(minute) // set to new value
  20. minute.equals(second) // -> true
  21. minute.toString() // -> "1:00.000"
  22. // `timeExpressions` are passed to Time or Timer, and can be an
  23. // object, number, array, or string (in multiple formats)
  24. // below are all the ways to write "25 minutes and 500 milliseconds"
  25. new Time('25:00.5'); // string in UTC-like syntax
  26. new Time('25m500ms'); // string with unit annotation
  27. new Time(1500500); // number for milliseconds
  28. new Time({ // object with full names
  29. minutes: 25,
  30. milliseconds: 500
  31. });

Ready to jump in? See the full Documentation site spec for many more uses and tutorials!

:clap: Supporters

Stargazers repo roster for goodtimer
Forkers repo roster for goodtimer