-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 49c375d
Showing
8 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Copyright (c) 2021 Alvin Ramskogler | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Timeout and Interval | ||
Implementation of setTimeout and setInterval with pause. | ||
|
||
**This package is currently NOT tested, I would love to hear your feedback!! Feel free to [open an issue](issues/new)!** | ||
|
||
## Instalation | ||
|
||
Run `npm install --save date-timeout-interval` | ||
|
||
|
||
## Usage | ||
Import the library to your script like this: | ||
```ts | ||
const { Timeout, Interval } = require("date-timeout-interval"); // JavaScript | ||
import { Timeout, Interval } from "date-timeout-interval"; // TypeScript | ||
``` | ||
If you want global classes, you can do the following: | ||
```ts | ||
require("date-timeout-interval/global"); // JavaScript | ||
import "date-timeout-interval/global"; // TypeScript | ||
``` | ||
This will add `Timeout` and `Interval` to `Date`. | ||
|
||
|
||
## Documentation | ||
|
||
### `new Timeout(callback: () => void, timeMS: number, autoStart: bool = false)` | ||
`callback: () => void` - Function to execute when time is over. `Timeout` instance will be applied as `this`.<br /> | ||
`timeMS: number` - Time in milliseconds, after which the callback should fire. | ||
`autoStart: bool` - If the timer should automatically start. This will call `start` internally. | ||
|
||
* `start(): this` | ||
|
||
Starts the timer. If the timer is currently paused, it will resume the timer. | ||
|
||
* `stop(): this` | ||
|
||
Stops the timer and resets it to 0. Acts like `clearTimeout`. | ||
|
||
* `pause(): this` | ||
|
||
Pauses the timer without reseting it. This is the main functionality of this class. | ||
|
||
* `state: 0 | 1 | 2 | 3` | ||
|
||
Defines the current state of the timer. | ||
| State | Description | | ||
|:-----:| ----------- | | ||
| 0 | The timer is currently reset. This state occurs when creating the class without auto-start or after `stop` is called. | | ||
| 1 | The timer is running. It can only be triggered by start, or by using auto-start while creating the class. | | ||
| 2 | The timer has been paused by calling pause. | | ||
| 3 | The timer has ended and fired the callback. | | ||
|
||
* `currentTime: number` | ||
|
||
The milliseconds the timer is currently set to. **Not the time left!** | ||
|
||
* `timeLeft: number` | ||
|
||
This will return the time left for the timer to end. **Warning: This will call a getter which will calculate the time, don't use this too often.** | ||
|
||
### `new Interval(callback: () => void, timeMS: number, autoStart)` | ||
`callback` - Function to execute in specific intervals. `Interval` instance will be applied as `this`.<br /> | ||
`timeMS` - Interval time in milliseconds. | ||
`autoStart: bool` - If the timer should automatically start. This will call `start` internally. | ||
|
||
* `start(): this` | ||
|
||
Starts the interval. If the interval is currently paused, it will resume the interval. | ||
|
||
* `stop(): this` | ||
|
||
Stops the interval and resets it to 0. Acts like `clearInterval`. | ||
|
||
* `pause(): this` | ||
|
||
Pauses the interval without reseting it. This will also remember the time left to the next call, so if you resume it again, it will continue at the position it stopped. | ||
|
||
* `state: 0 | 1 | 2` | ||
|
||
Defines the current state of the interval. These are the same as with the timeout, but there is no "end" state, because the interval will never end. | ||
| State | Description | | ||
|:-----:| ----------- | | ||
| 0 | The interval is currently reset. This state occurs when creating the class without auto-start or after `stop` is called. | | ||
| 1 | The interval is running. It can only be triggered by start, or by using auto-start while creating the class. | | ||
| 2 | The interval has been paused by calling pause. | | ||
|
||
* `currentTime: number` | ||
|
||
The milliseconds the interval is currently set to. **Not the time left until next execution!** | ||
|
||
* `timeLeft: number` | ||
|
||
This will return the time left for the next execution to happen. **Warning: This will call a getter which will calculate the time, don't use this too often.** | ||
|
||
# Licence | ||
This project is licenced under the MIT licence. Read it [here](LICENCE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Timeout, Interval } from "./index.d.ts"; | ||
|
||
interface Date { | ||
static Timeout: Timeout; | ||
static Interval: Interval; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var ti = require("./"); | ||
Date.Timeout = ti.Timeout; | ||
Date.Interval = ti.Interval; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export declare class Timeout { | ||
public start(): this; | ||
public stop(): this; | ||
public pause(): this; | ||
public readonly state: 0 | 1 | 2 | 3; | ||
public readonly currentTime: number; | ||
public readonly timeLeft: number; | ||
} | ||
export declare class Interval { | ||
public start(): this; | ||
public stop(): this; | ||
public pause(): this; | ||
public readonly state: 0 | 1 | 2; | ||
public readonly currentTime: number; | ||
public readonly timeLeft: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
function Timeout(callback, time, autoStart = false) { | ||
var self = this; | ||
this._callback = function() { self.state = 3; callback(); } | ||
this.currentTime = time; | ||
this._startedAt = 0; | ||
this._timeLeft = time; | ||
this._timerId = -1; | ||
this.state = 0; | ||
if (autoStart) this.start(); | ||
}; | ||
Timeout.prototype.start = function start(time) { | ||
if (arguments.length > 0) { // warning: undocumented feature, not really working | ||
this.currentTime = time; | ||
} | ||
if (this._timerId == -1) { | ||
if (this.state == 2) { | ||
this._startedAt = Date.now(); | ||
this._timerId = setTimeout(this._callback, this._timeLeft); | ||
this._state = 1; | ||
} | ||
else if (this.state == 0) { | ||
this._startedAt = Date.now(); | ||
this._timerId = setTimeout(this._callback, this.currentTime); | ||
this._state = 1; | ||
} | ||
} | ||
}; | ||
Timeout.prototype.pause = function pause() { | ||
if (this.state != 1) return; | ||
clearTimeout(this._timerId); | ||
this._timerId = -1; | ||
this._timeLeft -= Date.now() - this._startedAt; | ||
this.state = 2; | ||
}; | ||
Timeout.prototype.stop = function stop() { | ||
clearTimeout(this._timerId); | ||
this._timerId = -1; | ||
this._timeLeft = 0; | ||
this.state = 0; | ||
}; | ||
Object.defineProperty(Timeout.prototype, "timeLeft", { | ||
get: function() { | ||
if (this.state == 0) return 0; | ||
return Date.now() - this._startedAt; | ||
} | ||
}); | ||
|
||
function Interval(callback, time, autoStart = false) { | ||
var self = this; | ||
this._callback = function() { self._lastTrigger = Date.now(); callback(); }; | ||
this.currentTime = time; | ||
this._lastTrigger = 0; | ||
this._timeLeft = time; | ||
this._timerId = -1; | ||
this.state = 0; | ||
this._isInTimeout = false; | ||
if (autoStart) this.start(); | ||
}; | ||
Interval.prototype.start = function start() { | ||
if (this._timerId == -1) { | ||
if (this.state == 2) { | ||
this._lastTrigger = Date.now(); | ||
var self = this; | ||
this._timerId = setTimeout(function () { | ||
this._isInTimeout = false; | ||
this._timerId = setInterval(self._callback, self.currentTime); | ||
}, this._timeLeft); | ||
this._state = 1; | ||
this._isInTimeout = true; | ||
} | ||
else if (this.state == 0) { | ||
this._lastTrigger = Date.now(); | ||
this._timerId = setInterval(this._callback, this.currentTime); | ||
this._state = 1; | ||
} | ||
} | ||
}; | ||
Interval.prototype.pause = function pause() { | ||
if (this.state != 1) return; | ||
this._isInTimeout ? clearTimeout(this._timerId) : clearInterval(this._timerId); | ||
this._timerId = -1; | ||
this._timeLeft -= Date.now() - this._lastTrigger; | ||
this.state = 2; | ||
}; | ||
Interval.prototype.stop = function stop() { | ||
this._isInTimeout ? clearTimeout(this._timerId) : clearInterval(this._timerId); | ||
this._timerId = -1; | ||
this._timeLeft = 0; | ||
this.state = 0; | ||
}; | ||
Object.defineProperty(Interval.prototype, "timeLeft", { | ||
get: function() { | ||
if (this.state == 0) return 0; | ||
return Date.now() - this._lastTrigger; | ||
} | ||
}); | ||
|
||
|
||
module.exports = { Timeout: Timeout, Interval: Interval }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "date-timeout-interval", | ||
"version": "1.0.0", | ||
"description": "Implementation of setTimeout and setInterval with pause", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/blaumeise20/date-timeout-interval.git" | ||
}, | ||
"author": "Alvin Ramskogler", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/blaumeise20/date-timeout-interval/issues" | ||
}, | ||
"homepage": "https://github.com/blaumeise20/date-timeout-interval#readme" | ||
} |