{"id":22090705,"url":"https://github.com/remcoder/chronos","last_synced_at":"2025-06-12T07:05:04.078Z","repository":{"id":24240651,"uuid":"27633597","full_name":"remcoder/chronos","owner":"remcoder","description":"Reactive time utilities for Meteor. Includes reactive replacements for new Date() and moment().","archived":false,"fork":false,"pushed_at":"2016-07-29T23:00:19.000Z","size":40,"stargazers_count":37,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-11T08:53:29.300Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/remcoder.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-06T12:29:15.000Z","updated_at":"2024-04-11T08:53:29.301Z","dependencies_parsed_at":"2022-08-22T05:40:27.856Z","dependency_job_id":null,"html_url":"https://github.com/remcoder/chronos","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remcoder%2Fchronos","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remcoder%2Fchronos/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remcoder%2Fchronos/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remcoder%2Fchronos/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/remcoder","download_url":"https://codeload.github.com/remcoder/chronos/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227470021,"owners_count":17778930,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-01T02:17:15.961Z","updated_at":"2024-12-01T02:17:16.698Z","avatar_url":"https://github.com/remcoder.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Chronos [![Build Status](https://travis-ci.org/remcoder/chronos.svg?branch=master)](https://travis-ci.org/remcoder/chronos)\n\nFor a short introduction, see my [lightning talk at the Meteor Meetup](http://vimeo.com/129601361) in Amsterdam about Chronos.\n\n## Installation\n\n```bash\n$ meteor add remcoder:chronos\n```\n\n### API overview\n\n * __Chronos.date__ - a reactive replacement for `new Date()`\n * __Chronos.now__  - a reactive replacement for `Date.now()`\n * __Chronos.moment__ - a reactive replacement for `moment()` \n * __Chronos.update__ - trigger reactive updates with a single call \n * __Chronos.Timer__ - a simple reactive timer\n\n## Chronos.date(interval)\nA reactive replacement for `new Date`. It returns a `Date` object and triggers reactive updates.\nOptionally pass an `interval` in milliseconds. The default is 1000ms (1 second).\n\nUsage:\n\n```html\n\u003ctemplate name=\"foo\"\u003e\n   current time: {{currentTime}}\n\u003c/template\u003e\n```\n\n```javascript\nTemplate.foo.helpers({\n   currentTime : function() {\n       return Chronos.date(); // updates every second\n   }\n});\n```\n\n## Chronos.now(interval)\nA reactive replacement for `Date.now`. It returns the milliseconds since the start of the epoch and triggers reactive updates.\nOptionally pass an `interval` in milliseconds. The default is 1000ms (1 second).\n\nUsage:\n\n```html\n\u003ctemplate name=\"foo\"\u003e\n   millis: {{millis}}\n\u003c/template\u003e\n```\n\n```javascript\nTemplate.foo.helpers({\n   millis : function() {\n       return Chronos.now(); // updates every second\n   }\n});\n```\n\n## Chronos.moment(args...)\nA reactive replacement for the global function `moment()` as provided by [moment.js](http://momentjs.com/). This reactive version will trigger live updates, for live timestamps and such.\n You'll need to include moment.js yourself (and the reason is that there are [several different versions of momentjs on Atmosphere](https://atmospherejs.com/?q=moment)).\n\nUsage:\n\n```javascript\n// call with the same params as moment()\nChronos.moment(/* arguments */); \n```\n \nExample template + helper:\n\n```html\n\u003ctemplate name=\"foo\"\u003e\n\t\t\u003cdiv\u003etime spent: {{timeSpent}}\u003c/div\u003e\n\u003c/template\u003e\n```\n\n```javascript\nvar start = new Date();\n\nTemplate.foo.helpers({\n\t\ttimeSpent : function() {\n    \t\treturn Chronos.moment(start).fromNow();\n\t\t}\n});\n```\n\nExample with autorun:\n\n```javascript\n\tvar timestamp = new Date();\n\t\n\tTracker.autorun(function() {\n\t\t// prints how long ago the timestamp was made, every second\n\t\tconsole.log(Chronos.moment(timestamp).fromNow());\n\t});\n```\n\t\n_Note: this uses a `Chronos.Timer` under the hood. This timer is started automatically when you call `.liveMoment`_\n\n## Chronos.update(interval)\nWhen called from inside a Blaze helper or other reactive context, it will setup a timer once and make the context dependent on the timer. What this means is that for example the helper will we re-run every time the timer updates.\n\nUsage:\n\n```javascript\n// make context live updating. defaults to an interval of 1000m.\nChronos.update(interval);\n```\n\n_It returns the `Chronos.Timer` that drives the updates._\n\t\nExample template + helper:\n\n```html\t\n\u003ctemplate name=\"foo\"\u003e\n\t\u003cdiv\u003erandom number: {{randomNumber}}\u003c/div\u003e\n\u003c/template\u003e\n```\t\n\n```javascript\nTemplate.foo.helpers({\n\t\n\t// returns a random number between 0 and 10, every second\n\trandomNumber : function() {\n\t\tChronos.update();\n\t\treturn Math.round( Math.random() * 10 );\n\t}\n});\n```\n\nExample with autorun:\n\n```javascript\n// this will create counter and logs it every second\nvar count = 0;\n\t\nTracker.autorun(function() {\n\tChronos.update();\n\tconsole.log(count);\n\tcount++;\n});\n```\n\t\n_Note: this uses a `Chronos.Timer` under the hood. This timer is started automatically when you call `.update`_\n\n\n \n## Chronos.Timer()\nusage:\n\n```javascript\n// create new timer. defaults to an interval of 1000ms\nvar timer = new Chronos.Timer(interval);\n```\n\t\nThe timer exposes the `time` property, which is a [ReactiveVar](http://docs.meteor.com/#/full/reactivevar) and it holds the current time.\nGetting the time value is reactive so it will trigger re-runs whenever the timer produces an update.\n\n```javascript\ntimer.time.get();\n```\n\nExample template + helper:\n\n```html\t\n\u003ctemplate name=\"timer\"\u003e\n\t\u003cdiv class=\"timer\"\u003e{{time}}\u003c/div\u003e\n\u003c/template\u003e\n```\n\n```javascript\t\nvar timer = new Chronos.Timer(100);\n\nTemplate.timer.helpers({\n\t\n\t// counts from 0 to 10 in 10 seconds\n\ttime: function () {\n\t          return ((timer.time.get() // get the current time\n\t          / 1000)                   // convert ms to seconds\n\t          % 10)\t\t\t\t\t\t// reset every 10 seconds\n\t          .toFixed(0);\t\t\t\t// drop any decimals\n\t}\t\n});\n```\n\nExample with autorun: \t\n\n```javascript\n// prints the current time every 2 seconds\nvar timer = new Chronos.Timer(2000);\n\t\nTracker.autorun(function() {\n\tconsole.log(timer.time.get());\n});\n\t\ntimer.start();\n```\n\n### Chronos.Timer.start()\nStarts the timer (by kicking off a setInterval loop). \n\nUsage:\n\n```javascript\ntimer.start();\n```\t\n\n### Chronos.Timer.stop()\nStops the timer.\n\nUsage:\n\n```javascript\ntimer.stop();\n```\t\n\n## License\n\n[MIT License](LICENSE.txt)\n\n## Changelog\n - 0.5.0\n     - add new method `Chronos.now()`, which is a reactive replacement for `Date.now()`\n     - fixed a build error which prevented Chronos to work on windows (there was a colon in a filename)\n     - renamed some methods to stay closer to their non-reactive counterparts (with aliases in place to prevent breakage):\n     \t- Chronos.liveMoment() -\u003e Chronos.moment()\n     \t- Chronos.liveUpdate() -\u003e Chronos.update()\n     \t- Chronos.currentTime() -\u003e Chronos.date()\n - 0.4.1\n \t- fixed regression where the momentjs package from Atmosphere didn't work anymore\n - 0.4.0\n    - fixed a bug causing a ReferenceError when `liveMoment` is called and moment is imported as a module\n  ([#10](https://github.com/remcoder/chronos/issues/10))\n  thx to [dylanmcgowan](https://github.com/dylanmcgowan)\n - 0.3.1\n \t - fixed a bug where `destroy()` would sometimes be called twice, resulting in a TypeError ([#7](https://github.com/remcoder/chronos/issues/7))\n \t thx to [MichelFloyd](https://github.com/MichelFloyd)\n - 0.3.0\n\t - added `currentTime()` a reactive replacement for `new Date`\n   \t - no longer throws an exception when used outside a reactive context.\n - 0.2.x\n \t- bugfixes\n - 0.2.0\n \t- Instantiating a Chronos.Timer will not start the timer immediately anymore.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fremcoder%2Fchronos","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fremcoder%2Fchronos","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fremcoder%2Fchronos/lists"}