{"id":33169550,"url":"https://github.com/xenohunter/timestore","last_synced_at":"2025-11-20T19:00:59.440Z","repository":{"id":152866351,"uuid":"69187428","full_name":"xenohunter/timestore","owner":"xenohunter","description":"Multiple timers manager, especially good for games","archived":false,"fork":false,"pushed_at":"2016-10-20T22:48:52.000Z","size":37,"stargazers_count":44,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-05T22:06:45.249Z","etag":null,"topics":["gamedev","timers"],"latest_commit_sha":null,"homepage":"https://xenohunter.github.io/timestore","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/xenohunter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-09-25T20:01:15.000Z","updated_at":"2023-03-27T20:43:12.000Z","dependencies_parsed_at":"2024-01-12T00:25:52.846Z","dependency_job_id":"0e09d164-89da-4897-bb73-f8f94be255f2","html_url":"https://github.com/xenohunter/timestore","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/xenohunter/timestore","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xenohunter%2Ftimestore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xenohunter%2Ftimestore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xenohunter%2Ftimestore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xenohunter%2Ftimestore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xenohunter","download_url":"https://codeload.github.com/xenohunter/timestore/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xenohunter%2Ftimestore/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":285494607,"owners_count":27181443,"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","status":"online","status_checked_at":"2025-11-20T02:00:05.334Z","response_time":54,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["gamedev","timers"],"created_at":"2025-11-16T01:00:33.530Z","updated_at":"2025-11-20T19:00:59.434Z","avatar_url":"https://github.com/xenohunter.png","language":"JavaScript","funding_links":["https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=GQ5TJ6PCRXS4Y"],"categories":["Uncategorized"],"sub_categories":["Uncategorized"],"readme":"# timestore [![npm version](https://badge.fury.io/js/timestore.svg)](https://www.npmjs.com/package/timestore) [![downloads/month](https://img.shields.io/npm/dm/timestore.svg)](https://www.npmjs.com/package/timestore) [![PayPal donate button](https://img.shields.io/badge/paypal-donate-yellow.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=GQ5TJ6PCRXS4Y)\n\nManage multiple collections of timers: set, clear, pause and resume them. Use it with your [game states](http://phaser.io/news/2015/06/using-states-tutorial).\n\n## Installation\n\n    $ npm install timestore\n\nor, get a [browserified and minified version](https://cdn.rawgit.com/xenohunter/timestore/master/timestore.1.2.0.min.js).\n\nAlso, check out the [example page](https://xenohunter.github.io/timestore/).\n\n## Quick Guide\n\nLet's create a timestore:\n\n    var gameTimers = new timestore.Timestore();\n\nThat's how simple timers work:\n\n    var timeout = gameTimers.setTimeout(someCallback, 1000),\n        interval = gameTimers.setInterval(someRepeatingCallback, 100);\n\n    // After some time:\n    interval.clear();\n\nThat's how they work, too:\n\n    var bomb = gameTimers.setTimeout(bombExplosion, 5000);\n\n    someButton.on('click', function () {\n        if (bomb.getTimeLeft() \u003e 1000) {\n            bomb.pause();\n        } else {\n            console.log('Too late!');\n        }\n    });\n\nNow, the coolness begins:\n\n    function Unit() {\n        this.id = 'u' + getUniqueId();\n        gameTimers.setInterval(this.id + 'shoot', this.shoot.bind(this), 500);\n    }\n\n    Unit.prototype.shoot = function () {\n        // Makes a shot every 500 ms.\n    };\n\n    Unit.prototype.kill = function () {\n        gameTimers.clearInterval(this.id + 'shoot');\n    };\n\nMore than that, if your game has levels, you don't have to clear all timers by hand:\n\n    gameTimers.clearAll(); // It will clear both timeouts and intervals.\n    currentLevel = new Level();\n\nIf there are, say, locations in your game, and you want to freeze their states, you're welcome:\n\n    gameTimers.pauseAll();\n    // Player is in different location.\n    gameTimers.resumeAll();\n\nThere are two ways to interact with a timer:\n\n    var interval = gameTimers.setInterval(callback, 50);\n\n    // Directly invoke its methods:\n    interval.pause();\n    interval.resume();\n\n    // Or, manipulate them by ID:\n    gameTimers.pauseInterval(interval.id);\n    gameTimers.clearInterval(interval.id);\n\nThe second way may seem a bit strange, but it is very useful, indeed:\n\n    var interval = gameTimers.setInterval('someId', ohThatCallbackAgain, 200);\n\n    // And you can reach your interval from the farthest corner of your code:\n    gameTimers.pauseInterval('someId');\n\nIn the standard, there is an opportunity to pass optional arguments in the native method: `setTimeout(callback, delay, optArg1, optArg2)`. The syntax in timestore is different but powerful. There are two ways to pass optional arguments:\n\n    var timeout = gameTimers.setTimeout(callback, 50);\n    timeout.callWith(arg1, arg2, arg3); // You can choose between this...\n    timeout.applyWith([arg1, arg2, arg3]); // ...and this.\n\nAnd there's the third one which allows to set `this` context for the callback:\n\n    var timeout = gameTimers.setTimeout(someObj.someMethod, 50);\n    timeout.setThis(someObj);\n\nAll three methods work for intervals, too.\n\nYou also can use chaining for more natural code:\n\n    var interval = gameTimers.setInterval(callback, 50).callWith('give', 'drain').setThis(someObject);\n    interval.applyWith(['drain', 'gain']); // You can always rewrite optional arguments.\n\nTimeouts and intervals are stored aside from each other so the same IDs can be used for two of them. Though I don't recommend to do so due to the bad readability of the code with somehow doubling IDs.\n\n**NOTE**: *do not use numbers (and strings with just numbers in them, too) as IDs* because numbers are used as the inner standard for IDs.\n\n## API\n\n### Class: Timeout\n\nSyntax: `new Timeout(callback, delay, [fireBeforeClear], [id], [onClear])`. The last 2 arguments are used internally but will cause no harm if are passed.\n\n* *function* `callback` is just a callback, like in the native `setTimeout`\n* *number* `delay` is simply a number of milliseconds before the callback invocation\n* *boolean* `fireBeforeClear` forces the callback to be invoked if the timeout is explicitly cleared\n* *string* `id` is used by Timestore class to know all timeouts by \"name\"\n* *function* `onClear` is called when a timeout ends or is explicitly cleared (it is used to remove timeouts from the store)\n\nAll methods return `this` and therefore **can be chained** (except for `.clear()` and those which return exact values, like `.getTimeLeft()`).\n\n#### Timeout.setThis(thisArg)\n\nSets `this` context for `callback`.\n\n#### Timeout.callWith(arg1, arg2, ...)\n\nSaves arguments to pass them into `callback` while it's invoked. Rewrites old arguments if they exist.\n\n#### Timeout.applyWith(args)\n\nSaves `args` as arguments to pass them into `callback` while it's invoked. Rewrites old arguments if they exist.\n\n#### Timeout.clear()\n\nSimply clears the timeout. If `Timeout.fireBeforeClear` is set to `true` its callback will be invoked.\n\n#### Timeout.pause()\n\nPauses the timeout, causes no side effects.\n\n#### Timeout.resume()\n\nResumes the timeout. If `Timeout.delay` has been changed so to match a point in the past, the callback will be invoked immediately.\n\n#### Timeout.toggle()\n\nPauses or resumes the timeout depending on its current state.\n\n#### Timeout.changeDelay(newDelay)\n\nChanges `Timeout.delay`. If it's changed so to match a point in the past, the callback will be invoked immediately.\n\n#### Timeout.getTimeLeft()\n\nReturns the time left to the callback invocation.\n\n### Class: Interval\n\nSyntax: `new Interval(callback, delay, [fireBeforeClear], [id], [onClear])`. The last 2 arguments are used internally but will cause no harm if are passed.\n\n* *function* `callback` is just a callback, like in the native `setInterval`; it will be called every `delay` milliseconds\n* *number* `delay` is simply a number of milliseconds before the next callback invocation\n* *boolean* `fireBeforeClear` forces the callback to be called if an interval is explicitly cleared\n* *string* `id` is used by Timestore class to know all intervals by \"name\"\n* *function* `onClear` is called when an interval is explicitly cleared (it is used to remove intervals from the store)\n\nAll methods return `this` and therefore **can be chained** (except for `.clear()` and those which return exact values, like `.getTimeLeft()`).\n\n#### Interval.setThis(thisArg)\n\nSets `this` context for `callback`.\n\n#### Interval.callWith(arg1, arg2, ...)\n\nSaves arguments to pass them into `callback` while it's invoked (every time). Rewrites old arguments if they exist.\n\n#### Interval.applyWith(args)\n\nSaves `args` as arguments to pass them into `callback` while it's invoked (every time). Rewrites old arguments if they exist.\n\n#### Interval.clear()\n\nClears the interval. If `Interval.fireBeforeClear` is set to `true` its callback will be invoked (for the last time).\n\n#### Interval.pause()\n\nPauses the interval without side effects.\n\n#### Interval.resume()\n\nResumes the interval. If `Interval.delay` has been changed so to match a point in the past, the callback will be invoked immediately.\n\n#### Interval.toggle()\n\nPauses or resumes the interval depending on its current state.\n\n#### Interval.changeDelay(newDelay)\n\nChanges `Interval.delay`. If it's changed so to match a point in the past, the callback will be invoked immediately. That change affects all callback invocations, not only the current one.\n\n#### Interval.getTimeLeft()\n\nReturns the time left to the next callback invocation.\n\n### Class: Timestore\n\nSyntax: `new Timestore()`.\n\n#### Timestore.setTimeout([id], callback, delay, fireBeforeClear)\n\nReturns a `Timeout` object.\n\n* *string* `id` is optional; if there is already a timeout with the same ID, it will be cleared and overwritten\n\n**NOTE**: don't use numbers as IDs.\n\n#### Timestore.clearTimeout(id), Timestore.pauseTimeout(id), Timestore.resumeTimeout(id), Timestore.resumeTimeout(id), Timestore.changeTimeoutDelay(id, newDelay), Timestore.getTimeoutTimeLeft(id)\n\nIf there is a timeout with a given `id`, calls the corresponding method of that timeout. Passes the arguments, if any. Returns the return value of that method, if any.\n\n#### Timestore.clearTimeouts(idArray), Timestore.pauseTimeouts(idArray), Timestore.resumeTimeouts(idArray), Timestore.resumeTimeouts(idArray)\n\nCalls the corresponding method of all the timeouts which IDs are listed in `idArray`.\n\n#### Timestore.setInterval([id], callback, delay, fireBeforeClear)\n\nReturns a `Interval` object.\n\n* *string* `id` is optional; if there is already an interval with the same ID, it will be cleared and overwritten\n\n**NOTE**: don't use numbers as IDs.\n\n#### Timestore.clearInterval(id), Timestore.pauseInterval(id), Timestore.resumeInterval(id), Timestore.resumeInterval(id), Timestore.changeIntervalDelay(id, newDelay), Timestore.getIntervalTimeLeft(id)\n\nIf there is an interval with a given `id`, calls the corresponding method of that interval. Passes the arguments, if any. Returns the return value of that method, if any.\n\n#### Timestore.clearIntervals(idArray), Timestore.pauseIntervals(idArray), Timestore.resumeIntervals(idArray), Timestore.resumeIntervals(idArray)\n\nCalls the corresponding method of all the intervals which IDs are listed in `idArray`.\n\n#### Timestore.clearAll()\n\nClears all timeouts and intervals. Resets inner ID counters to zero. Empties the current timestore. All timeouts and intervals with `fireBeforeClear` invoke their callbacks.\n\n#### Timestore.pauseAll()\n\nPauses all timeouts and intervals without side effects.\n\n#### Timestore.resumeAll()\n\nResumes all timeouts and intervals. For those which `Interval.delay` has been changed so to match a point in the past, the callbacks will be invoked immediately.\n\n#### Timestore.getTimeouts()\n\nReturns an array of timeout IDs.\n\n#### Timestore.hasTimeout(id)\n\nReturns `true` if there is a timeout with a given `id`. Otherwise, returns `false`.\n\n#### Timestore.getIntervals()\n\nReturns an array of interval IDs.\n\n#### Timestore.hasInterval(id)\n\nReturns `true` if there is an interval with a given `id`. Otherwise, returns `false`.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxenohunter%2Ftimestore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxenohunter%2Ftimestore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxenohunter%2Ftimestore/lists"}