{"id":30340703,"url":"https://github.com/aasim-a/asynctimer","last_synced_at":"2025-08-18T08:19:16.559Z","repository":{"id":39651649,"uuid":"321950242","full_name":"Aasim-A/AsyncTimer","owner":"Aasim-A","description":"JavaScript-like Async timing functions (setTimeout, setInterval) for Arduino, ESP8266, ESP32 and other compatible boards","archived":false,"fork":false,"pushed_at":"2024-09-18T06:08:03.000Z","size":41,"stargazers_count":48,"open_issues_count":1,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-18T08:29:39.111Z","etag":null,"topics":["arduino","asynctimer","callbackfunction","esp32","esp32-arduino","esp8266","esp8266-arduino","javascript","setinterval","settimeout"],"latest_commit_sha":null,"homepage":"","language":"C++","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/Aasim-A.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-16T10:42:16.000Z","updated_at":"2024-09-18T06:06:41.000Z","dependencies_parsed_at":"2022-09-20T07:53:25.511Z","dependency_job_id":null,"html_url":"https://github.com/Aasim-A/AsyncTimer","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/Aasim-A/AsyncTimer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aasim-A%2FAsyncTimer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aasim-A%2FAsyncTimer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aasim-A%2FAsyncTimer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aasim-A%2FAsyncTimer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Aasim-A","download_url":"https://codeload.github.com/Aasim-A/AsyncTimer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aasim-A%2FAsyncTimer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270962648,"owners_count":24676021,"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-08-18T02:00:08.743Z","response_time":89,"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":["arduino","asynctimer","callbackfunction","esp32","esp32-arduino","esp8266","esp8266-arduino","javascript","setinterval","settimeout"],"created_at":"2025-08-18T08:19:14.386Z","updated_at":"2025-08-18T08:19:16.543Z","avatar_url":"https://github.com/Aasim-A.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AsyncTimer\r\n\r\n[![arduino-library-badge](https://www.ardu-badge.com/badge/AsyncTimer.svg?)](https://www.ardu-badge.com/AsyncTimer)\r\n[![GitHub release](https://img.shields.io/github/release/Aasim-A/AsyncTimer.svg)](https://github.com/Aasim-A/AsyncTimer/releases)\r\n[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/Aasim-A/AsyncTimer/blob/master/LICENSE)\r\n[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing)\r\n[![GitHub issues](https://img.shields.io/github/issues/Aasim-A/AsyncTimer.svg)](http://github.com/Aasim-A/AsyncTimer/issues)\r\n\r\n### JavaScript-like Async timing functions (setTimeout, setInterval) for Arduino, ESP8266, ESP32 and other compatible boards\r\n\r\n# Installing\r\n\r\n## Arduino IDE:\r\n\r\n#### Library Manager:\r\n\r\nThe easiest way is to install it through Arduino Library manager selecting the menu:\r\n```\r\nSketch -\u003e Include Library -\u003e Manage Libraries\r\n```\r\nThen type `AsyncTimer` into the search box and install the latest version.\r\n\r\n#### Manual install:\r\n\r\nDownload the repository as .zip and include it as a new library into the IDE selecting the menu:\r\n\r\n```\r\n Sketch -\u003e Include Library -\u003e Add .Zip library\r\n```\r\n\r\n## PlatformIO:\r\n\r\nGo to libraries and type [AsyncTimer](https://platformio.org/lib/show/11569/AsyncTimer) into the search bar and add it to your project.\r\n\r\n\r\n# Getting Started\r\nSimply include the library into your sketch and make one instance of `AsyncTimer` ~~and add the setup function to `void setup()`~~ and the handler to `void loop()` and then start using it!\r\n\r\n\r\n#### Example:\r\n\r\n```c++\r\n#include \u003cAsyncTimer.h\u003e\r\n\r\nAsyncTimer t;\r\n\r\nvoid loop()\r\n{\r\n  t.handle();\r\n}\r\n```\r\n\r\n\u003e ⚠NOTE⚠: only one instance must be created and it must be outside any function, as shown in the example above.\r\n\r\n\u003e ⚠NOTE⚠: The default timer capacity is 10, if you wish to increase it, you have to use non-default initializer:\r\n\r\n```c++\r\nAsyncTimer t(22);\r\n```\r\n\r\n# API\r\n\r\n## setTimeout(callbackFunction, delayInMs)\r\n\r\nCreates a new timeout.\r\n\r\n`setTimeout` takes two arguments, the first one is the function to call after waiting, the second one is the time in milliseconds to wait before executing the function. It returns an `unsigned short` id of the timeout. If the `timeout` creation was unsuccessful, it returns `0`.\r\nIt will run only once unless canceled.\r\n\r\n#### Example:\r\n\r\n- Using lambda function:\r\n\r\n```c++\r\nAsyncTimer t;\r\n\r\nt.setTimeout([]() {\r\n  Serial.println(\"Hello world!\");\r\n}, 2000);\r\n// \"Hello world!\" will be printed to the Serial once after 2 seconds\r\n```\r\n\r\n- Using normal function:\r\n\r\n```c++\r\nAsyncTimer t;\r\n\r\nvoid functionToCall()\r\n{\r\n  Serial.println(\"Hello world!\");\r\n}\r\n\r\nt.setTimeout(functionToCall, 2000);\r\n// \"Hello world!\" will be printed to the Serial once after 2 seconds\r\n```\r\n\r\n## setInterval(callbackFunction, delayInMs)\r\n\r\nCreates a new interval.\r\n\r\n`setInterval` takes the same parameters as `setTimeout` and returns an `unsigned short` id of the interval, unlike `setTimeout`, it will keep executing the code forever unless canceled. If the `interval` creation was unsuccessful, it returns `0`.\r\n\r\n#### Example:\r\n\r\n- Using lambda function:\r\n\r\n```c++\r\nAsyncTimer t;\r\n\r\nt.setInterval([]() {\r\n  Serial.println(\"Hello world!\");\r\n}, 2000);\r\n// \"Hello world!\" will be printed to the Serial every 2 seconds\r\n```\r\n\r\n- Using normal function:\r\n\r\n```c++\r\nAsyncTimer t;\r\n\r\nvoid functionToCall()\r\n{\r\n  Serial.println(\"Hello world!\");\r\n}\r\n\r\nt.setInterval(functionToCall, 2000);\r\n// \"Hello world!\" will be printed to the Serial every 2 seconds\r\n```\r\n\r\n## getRemaining(intervalOrTimeoutId)\r\n\r\nGets the number of milliseconds remaining in a timer. Returns `0` if the timer is not found.\r\n\r\n`getRemaining` takes one argument, the `id` returned from `setTimeout` or `setInterval` function, returns `unsigned long`.\r\n\r\n#### Example:\r\n\r\n```c++\r\nAsyncTimer t;\r\n\r\nunsigned short timeoutId = t.setTimeout([]() {\r\n  Serial.println(\"Hello world!\");\r\n}, 3000);\r\n\r\n// Get the remaining ms on the timer\r\nunsigned long remaining = t.getRemaining(timeoutId);\r\n```\r\n\r\n## changeDelay(intervalOrTimeoutId, delayInMs)\r\nChanges the delay value of an active `intervalOrTimeout`.\r\n\r\n`changeDelay` takes two arguments, the `id` returned from `setTimeout` or `setInterval` function and the new `delayValue` in `ms`, returns `void`.\r\n\r\n#### Example:\r\n\r\n- Changing the delay of `setInterval`:\r\n\r\n```c++\r\nAsyncTimer t;\r\n\r\nunsigned short intervalId = t.setInterval([]() {\r\n  Serial.println(\"Hello world!\");\r\n}, 2000);\r\n\r\nt.setTimeout([=]() {\r\n  t.changeDelay(intervalId, 3500);\r\n  // Now the interval runs every 3500ms instead of the old 2000ms\r\n}, 7000);\r\n```\r\n\r\n## delay(intervalOrTimeoutId, delayInMs)\r\nDelays the execution of an active `intervalOrTimeout`.\r\n\r\n`delay` takes two arguments, the `id` returned from `setTimeout` or `setInterval` function and the new `delayValue` in `ms`, returns `void`.\r\n\r\n#### Example:\r\n\r\n- Delaying the execution of `setInterval`:\r\n\r\n```c++\r\nAsyncTimer t;\r\n\r\nunsigned short intervalId = t.setInterval([]() {\r\n  Serial.println(\"Hello world!\");\r\n}, 2000);\r\n\r\nt.setTimeout([=]() {\r\n  t.delay(intervalId, 3500);\r\n  // Now the interval will be delayed by an extra 3500ms,\r\n  // afterwords, it will continue executing normally.\r\n}, 7000);\r\n```\r\n\r\n## reset(intervalOrTimeoutId)\r\nResets the wait time of an active `intervalOrTimeout`.\r\n\r\n`delay` takes one argument, the `id` returned from `setTimeout` or `setInterval` function, returns `void`.\r\n\r\n#### Example:\r\n\r\n- Resetting the wait time of `setInterval`:\r\n\r\n```c++\r\nAsyncTimer t;\r\n\r\nunsigned short intervalId = t.setInterval([]() {\r\n  Serial.println(\"Hello world!\");\r\n}, 2000);\r\n\r\nt.setTimeout([=]() {\r\n  t.reset(intervalId);\r\n  // Now the interval will be reset, this means that it will\r\n  // execute exactly 2000ms after the reset function call.\r\n}, 7000);\r\n```\r\n\r\n## cancel(intervalOrTimeoutId)\r\n\r\nCancels the execution of a timeout or an interval.\r\n\r\n`cancel` takes one argument, the `id` returned from `setTimeout` or `setInterval` function and returns `void`.\r\n\r\n#### Example:\r\n\r\n- Cancelling an interval:\r\n\r\n```c++\r\nAsyncTimer t;\r\n\r\nunsigned short intervalId = t.setInterval([]() {\r\n  Serial.println(\"Hello world!\");\r\n}, 2000);\r\n\r\n// Cancel the interval after 7 seconds:\r\nt.setTimeout([=]() {\r\n  t.cancel(intervalId);\r\n}, 7000);\r\n```\r\n\r\n- Cancelling a timeout:\r\n\r\n```c++\r\nAsyncTimer t;\r\n\r\n// This timeout will never run\r\nunsigned short timeoutId = t.setTimeout([]() {\r\n  Serial.println(\"Hello world!\");\r\n}, 3000);\r\n\r\n// Cancel the timeout before it's executed\r\nt.cancel(timeoutId);\r\n```\r\n\r\n## cancelAll(includeIntervals = true)\r\n\r\nCancels the execution of a all timeouts and intervals or just timeouts.\r\n\r\n`cancelAll` takes one optional argument, if you intend to only cancel timeouts, then call the function with `false`, otherwise it clears both timeouts and intervals be default, returns `void`.\r\n\r\n#### Example:\r\n\r\n- Cancelling all timeouts and intervals:\r\n\r\n```c++\r\nAsyncTimer t;\r\n\r\nt.setInterval([]() {\r\n  Serial.println(\"foo\");\r\n}, 2000);\r\n\r\nt.setTimeout([]() {\r\n  Serial.println(\"bar\");\r\n}, 7000);\r\n\r\nt.setTimeout([]() {\r\n  Serial.println(\"baz\");\r\n}, 7000);\r\n\r\n// After this call, nothing will be running inside AsyncTimer\r\nt.cancelAll();\r\n```\r\n\r\n- Only cancelling timeouts:\r\n\r\n```c++\r\nAsyncTimer t;\r\n\r\nt.setInterval([]() {\r\n  Serial.println(\"foo\");\r\n}, 2000);\r\n\r\nt.setTimeout([]() {\r\n  Serial.println(\"bar\");\r\n}, 7000);\r\n\r\nt.setTimeout([]() {\r\n  Serial.println(\"baz\");\r\n}, 7000);\r\n\r\n// After this call, only intervals will be running inside AsyncTimer\r\nt.cancelAll(false);\r\n```\r\n\r\n# Examples\r\n\r\n- BlinkUsingInterval - Blink led using `setInterval`.\r\n- SerialMsgUsingTimeout - Send a message to the serial monitor using `setTimeout` 10 seconds after booting.\r\n- CancelInterval - Cancel an interval using `cancel`.\r\n- CancelTimeout - Cancel a timeout using `cancel`.\r\n- DebounceUsingTimeout - Debounce button using a `delay`.\r\n\r\n# License\r\n\r\nThis library is licensed under [MIT](https://github.com/Aasim-A/AsyncTimer/blob/master/LICENSE).\r\n\r\n# Copyright\r\n\r\nCopyright 2022 - Aasim-A\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faasim-a%2Fasynctimer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faasim-a%2Fasynctimer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faasim-a%2Fasynctimer/lists"}