|
1 | 1 |  |
2 | | -# Introduce |
3 | | -The 'WW Timer' is a timer for browsers that operate using web workers. |
4 | | -# Why need |
5 | | -Implementing a timer in a browser is challenging for a variety of reasons. |
6 | | -- Accurate time calculation may be difficult depending on the congestion level of the event loop. |
7 | | -- Browser operating in the background does not have 'setInterval' or 'setTimeout' working properly. |
| 2 | +# WW Timer |
| 3 | +WW Timer is a highly accurate timer library for web browsers. |
| 4 | + |
| 5 | +## Why It's Needed |
| 6 | +Typically, when implementing timer functionality in web browsers, the `setTimeout` or `setInterval` methods are used. However, due to the nature of JavaScript's execution model, achieving precise time measurement can be challenging. |
| 7 | + |
| 8 | +JavaScript's asynchronous functions are subject to delays depending on the number of tasks the web browser is executing. This can result in the `setTimeout` and `setInterval` functions being deprioritized in the event loop, which, while generally inconsequential for standard functionality, can introduce critical errors in timing accuracy for timer implementations. |
| 9 | + |
| 10 | +## Solution |
| 11 | +The solution lies in utilizing the [Web Worker API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) supported by browsers. Since Web Workers operate independently of the browser's main thread, they are not affected by the complexity of the tasks being executed in the web app. This independence allows for more accurate time measurements, irrespective of the main thread's load. |
| 12 | + |
| 13 | +## Features |
| 14 | +- **Accuracy**: Provides highly accurate timing by circumventing the event loop's congestion. |
| 15 | +- **Independence**: Operates separately from the main thread, ensuring timer precision is maintained. |
| 16 | +- **Ease of Use**: Designed to be straightforward to implement within any web application. |
| 17 | + |
| 18 | +## Installation |
| 19 | +```sh |
| 20 | +# npm |
| 21 | +npm install ww-timer |
| 22 | +# yarn |
| 23 | +yarn add ww-timer |
| 24 | +``` |
| 25 | + |
| 26 | +## Quick Start |
| 27 | +```javascript |
| 28 | +// Import WW Timer |
| 29 | +import WWTimer from 'ww-timer'; |
| 30 | + |
| 31 | +// Create a new timer |
| 32 | +const timer = new WWTimer(); |
| 33 | + |
| 34 | +// Start the timer with a callback function |
| 35 | +timer.start(() => { |
| 36 | + console.log('Timer tick'); |
| 37 | +}); |
| 38 | + |
| 39 | +// Pause the timer |
| 40 | +timer.pause(); |
| 41 | + |
| 42 | +// Destroy the timer instance |
| 43 | +timer.destroy(); |
| 44 | +``` |
| 45 | + |
| 46 | +## API Reference |
| 47 | +- `start(callback, interval)`: Starts the timer with the specified callback function and interval. |
| 48 | +- `pause()`: Stops the timer. |
| 49 | +- `destroy()`: Destroy the timer instance. |
| 50 | + |
| 51 | +## Compatibility |
| 52 | +Compatible with most modern web browsers that support the Web Workers API. |
| 53 | + |
| 54 | +## Contributing |
| 55 | +Contributions to the WW Timer project are welcome. Please refer to the CONTRIBUTING.md file for more details. |
| 56 | + |
| 57 | +## License |
| 58 | +WW Timer is released under the MIT License. See the LICENSE file for more information. |
0 commit comments