-
-
Notifications
You must be signed in to change notification settings - Fork 832
feat: add utils/delay
#547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Planeshifter
wants to merge
43
commits into
stdlib-js:develop
Choose a base branch
from
Planeshifter:utils/delay
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
3f0cadf
Add README.md
Planeshifter 04655c8
Add method and note
Planeshifter 9a75051
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 87112d6
Add README.md
Planeshifter 144f3ac
Add method and note
Planeshifter fe146fb
Scaffold utils/delay package files
stdlib-bot 391fc17
Apply suggestions from code review
kgryte 9f2d294
Add empty line
kgryte a1b2f5b
Fix missing line
kgryte 0dc9b83
Update copy
kgryte 717fa7b
Add link
kgryte 8b92d5e
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter c3844f9
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 4eda8e9
Add links and duration string notes
Planeshifter 700d40f
Add note and CLI section
Planeshifter c10b642
Update copy
kgryte 9d38f2b
Remove stray link
kgryte c39c6e6
Update copy
kgryte 41ff87f
Update copy
kgryte 1be78fc
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 98b62fa
Merge remote-tracking branch 'upstream/scaffold-547/utils/delay' into…
Planeshifter d12b782
Merge branch 'utils/delay' of https://github.com/Planeshifter/stdlib …
Planeshifter 15d3103
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter cada44d
Update lib/node_modules/@stdlib/utils/delay/lib/main.js
Planeshifter c27c475
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter e4aaa60
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter e789233
Update lib/node_modules/@stdlib/utils/delay/test/test.js
Planeshifter 6d40619
Update lib/node_modules/@stdlib/utils/delay/docs/repl.txt
Planeshifter 041fea8
Update lib/node_modules/@stdlib/utils/delay/docs/types/index.d.ts
Planeshifter c06e0aa
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 3ec6a90
fix: address feedback from code review
Planeshifter a70cb93
test: comment out tests for now
Planeshifter 9625289
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter aa562df
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 0d927c5
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 19fa81a
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 44fafb5
chore: update copyright years
stdlib-bot f494d7f
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter ddd0741
Merge branch 'develop' into utils/delay
stdlib-bot a2eae7e
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 1e84184
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 3ad1b47
Merge remote-tracking branch 'upstream/develop' into utils/delay
stdlib-bot 8faa10d
chore: format code in delay examples
Planeshifter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,206 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2022 The Stdlib Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
|
||
# delay | ||
|
||
> Invoke a function after a specified number of milliseconds. | ||
|
||
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
||
<section class="intro"> | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<!-- Package usage documentation. --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var delay = require( '@stdlib/utils/delay' ); | ||
``` | ||
|
||
#### delay( fcn, wait\[, ...args] ) | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Invokes a function after a specified number of milliseconds. | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```javascript | ||
function beep() { | ||
console.log( 'beep' ); | ||
} | ||
|
||
delay( beep, 3000 ); | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Logs 'beep' after three seconds... | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
The function accepts additional arguments to invoke the function with. | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```javascript | ||
function add( x, y ) { | ||
var sum = x + y; | ||
console.log( 'sum: %d', sum ); | ||
} | ||
|
||
delay( add, 3000, 2, 3 ); | ||
// Logs 'sum: 5' after three seconds... | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
The function returns a `timeout` object with the following properties and methods: | ||
|
||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#### timeout.clear() | ||
|
||
Clears a pending invocation of a function. | ||
|
||
```javascript | ||
function beep() { | ||
console.log( 'beep' ); | ||
} | ||
var t = delay( beep, 3000 ); | ||
t.clear(); | ||
``` | ||
|
||
#### timeout.invoke() | ||
|
||
Invokes a function immediately. | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```javascript | ||
function beep() { | ||
console.log( 'beep' ); | ||
} | ||
var t = delay( beep, 3000 ); | ||
t.invoke(); | ||
// Logs 'beep' immediately... | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
#### timeout.duration | ||
|
||
Timeout duration (in milliseconds) before invoking a function. | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```javascript | ||
function beep() { | ||
console.log( 'beep' ); | ||
} | ||
var t = delay( beep, 3000 ); | ||
var duration = t.duration; | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// returns 3000 | ||
``` | ||
|
||
#### timeout.status | ||
|
||
Integer indicating the status of a timeout. The following values are possible: | ||
|
||
- `0`: fuction has not yet been invoked. | ||
- `1`: function has been invoked. | ||
- `2`: function invocation has been cleared. | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```javascript | ||
function beep() { | ||
console.log( 'beep' ); | ||
} | ||
var t = delay( beep, 3000 ); | ||
var status = t.status; | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// returns 0 | ||
|
||
t.clear(); | ||
status = t.status; | ||
// returns 2 | ||
``` | ||
|
||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="notes"> | ||
|
||
## Notes | ||
|
||
- The function accepts additional arguments to invoke the function with. These arguments are provided to the supplied function in the same order as provided to the `delay` function. | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- In contrast to using `setTimeout`, which returns a timeout identifier that can be used to clear a pending invocation by calling `clearTimeout`, the `delay` function returns a `timeout` object with additional properties and methods. | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<!-- Package usage examples. --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var delay = require( '@stdlib/utils/delay' ); | ||
|
||
function beep() { | ||
console.log( 'beep' ); | ||
} | ||
function boop() { | ||
console.log( 'boop' ); | ||
} | ||
function baz() { | ||
console.log( 'baz' ); | ||
} | ||
|
||
var t1 = delay( beep, 3000 ); | ||
var t2 = delay( boop, 1000 ); | ||
var t3 = delay( baz, 4000 ); | ||
|
||
t1.clear(); | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Wait 3 seconds... | ||
// => beep | ||
|
||
// Wait 1 more second... | ||
// => baz | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="references"> | ||
|
||
</section> | ||
|
||
<!-- /.references --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</section> | ||
|
||
<!-- /.links --> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.