{"id":20438331,"url":"https://github.com/huang2002/timer-x","last_synced_at":"2025-06-20T12:33:47.561Z","repository":{"id":140534272,"uuid":"212322952","full_name":"huang2002/timer-x","owner":"huang2002","description":"A promise-based timer.","archived":false,"fork":false,"pushed_at":"2020-03-14T06:32:25.000Z","size":36,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-20T10:55:18.213Z","etag":null,"topics":["3h","async","promise","sleep","timer","wait"],"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/huang2002.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-02T11:31:30.000Z","updated_at":"2019-10-02T11:35:55.000Z","dependencies_parsed_at":null,"dependency_job_id":"64d88994-5c26-4f8a-bbd4-d4e9174d8b26","html_url":"https://github.com/huang2002/timer-x","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/huang2002/timer-x","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huang2002%2Ftimer-x","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huang2002%2Ftimer-x/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huang2002%2Ftimer-x/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huang2002%2Ftimer-x/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/huang2002","download_url":"https://codeload.github.com/huang2002/timer-x/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huang2002%2Ftimer-x/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260944939,"owners_count":23086919,"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":["3h","async","promise","sleep","timer","wait"],"created_at":"2024-11-15T09:10:31.216Z","updated_at":"2025-06-20T12:33:42.547Z","avatar_url":"https://github.com/huang2002.png","language":"JavaScript","readme":"# timer-x\n\n\u003e A promise-based timer.\n\n## TOC\n\n- [Introduction](#introduction)\n- [Usage](#usage)\n- [API Reference](#api-reference)\n- [Example](#example)\n- [Links](#links)\n\n## Introduction\n\n`timer-x` is a promise-based timer lib which provides some utils for time management.\n\n## Usage\n\n### npm\n\n1. Use npm to install it as a dependency:\n\n    ```bash\n    npm install timer-x\n    ```\n\n2. Import the exports of this lib:\n\n    ```js\n    import { /* ... */ } from \"timer-x\";\n    // or\n    const { /* ... */ } = require(\"timer-x\");\n    ```\n\n3. Use them in your code.\n\n### CDN\n\n1. Include one of the following script tags in your HTML file:\n\n    via jsdelivr:\n\n    ```html\n    \u003cscript type=\"text/javascript\" crossorigin=\"anonymous\" src=\"https://cdn.jsdelivr.net/npm/timer-x@latest/dist/timer-x.umd.min.js\"\u003e\u003c/script\u003e\n    ```\n\n    or via unpkg:\n\n    ```html\n    \u003cscript type=\"text/javascript\" crossorigin=\"anonymous\" src=\"https://unpkg.com/timer-x@latest/dist/timer-x.umd.min.js\"\u003e\u003c/script\u003e\n    ```\n\n2. Access the APIs via the `TX` global.\n\n    ```js\n    const { /* ... */ } = TX;\n    ```\n\nIf you want a specified version, just replace `latest` with that in the url. By the way, it is recommended to use a specified version in production.\n\nFor more information about these two CDN sites, visit [www.jsdelivr.com](https://www.jsdelivr.com) and [unpkg.com](https://unpkg.com).\n\n## API Reference\n\n```ts\n/**\n * @desc The class for timers.\n */\nclass Timer {\n\n    /**\n     * @desc Register a delayed task (using `setTimeout` internally)\n     * @returns a promise resolved after specific timeout with the given data\n     */\n    waitTimeout\u003cT = unknown\u003e(timeout: number, data?: T): Promise\u003cT\u003e;\n\n    /**\n     * @desc Register an animation-frame task (using `requestAnimationFrame` internally)\n     * @returns a promise resolved at next animation frame with the given data\n     */\n    waitAnimationFrame\u003cT = unknown\u003e(data?: T): Promise\u003cT\u003e;\n\n    /**\n     * @desc Remove the tasks (their promises will be rejected with the given reason)\n     */\n    abort(reason?: unknown): void;\n\n    /**\n     * @desc Flush the tasks (their promises will be resolved immediately)\n     */\n    flush(): void;\n\n    /**\n     * @desc Pause the tasks (their timing will be paused)\n     */\n    pause(): void;\n\n    /**\n     * @desc Resume paused tasks (their timing will be continued)\n     */\n    resume(): void;\n\n    /**\n     * @desc Reset the timing of the tasks\n     */\n    reset(): void;\n\n}\n\n/**\n * @desc Similar to `timer.waitTimeout` but simplified\n */\nfunction waitTimeout\u003cT = unknown\u003e(timeout: number, data?: T): Promise\u003cT\u003e;\n\n/**\n * @desc Similar to `timer.waitAnimationFrame` but simplified\n */\nfunction waitAnimationFrame\u003cT = unknown\u003e(data?: T): Promise\u003cT\u003e;\n```\n\n## Example\n\n```js\nTX.waitAnimationFrame()\n    .then(() =\u003e {\n        // ...\n    })\n    .then(TX.waitTimeout(1000))\n    .then(() =\u003e {\n        // ...\n        return TX.waitTimeout(500, data);\n    })\n    .then(data =\u003e {\n        // ...\n    });\n\nconst timer = new TX.Timer();\n\ntimer.waitTimeout(5000)\n    .then(() =\u003e {\n        // ...\n    })\n    .catch(error =\u003e {\n        console.error(error);\n    });\n\nfunction handleClick(id, listener) {\n    document.getElementById(id)\n        .addEventListener('click', listener);\n}\n\nhandleClick('pause', () =\u003e timer.pause());\nhandleClick('resume', () =\u003e timer.resume());\nhandleClick('reset', () =\u003e timer.reset());\nhandleClick('flush', () =\u003e timer.flush());\nhandleClick('abort', () =\u003e timer.abort('ABORT'));\n```\n\n## Links\n\n- [Contributing Guide](./CONTRIBUTING.md)\n- [Changelog](./CHANGELOG.md)\n- [License (MIT)](./LICENSE)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhuang2002%2Ftimer-x","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhuang2002%2Ftimer-x","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhuang2002%2Ftimer-x/lists"}