Skip to content
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

feat: add utils/delay #547

Open
wants to merge 43 commits into
base: develop
Choose a base branch
from
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 Oct 13, 2022
04655c8
Add method and note
Planeshifter Oct 13, 2022
9a75051
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Oct 13, 2022
87112d6
Add README.md
Planeshifter Oct 13, 2022
144f3ac
Add method and note
Planeshifter Oct 13, 2022
fe146fb
Scaffold utils/delay package files
stdlib-bot Oct 13, 2022
391fc17
Apply suggestions from code review
kgryte Oct 15, 2022
9f2d294
Add empty line
kgryte Oct 15, 2022
a1b2f5b
Fix missing line
kgryte Oct 15, 2022
0dc9b83
Update copy
kgryte Oct 15, 2022
717fa7b
Add link
kgryte Oct 15, 2022
8b92d5e
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Oct 20, 2022
c3844f9
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Nov 1, 2022
4eda8e9
Add links and duration string notes
Planeshifter Nov 1, 2022
700d40f
Add note and CLI section
Planeshifter Nov 1, 2022
c10b642
Update copy
kgryte Nov 2, 2022
9d38f2b
Remove stray link
kgryte Nov 2, 2022
c39c6e6
Update copy
kgryte Nov 2, 2022
41ff87f
Update copy
kgryte Nov 2, 2022
1be78fc
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Nov 2, 2022
98b62fa
Merge remote-tracking branch 'upstream/scaffold-547/utils/delay' into…
Planeshifter Nov 2, 2022
d12b782
Merge branch 'utils/delay' of https://github.com/Planeshifter/stdlib …
Planeshifter Nov 2, 2022
15d3103
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Feb 21, 2023
cada44d
Update lib/node_modules/@stdlib/utils/delay/lib/main.js
Planeshifter Feb 21, 2023
c27c475
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Feb 21, 2023
e4aaa60
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Feb 21, 2023
e789233
Update lib/node_modules/@stdlib/utils/delay/test/test.js
Planeshifter May 29, 2023
6d40619
Update lib/node_modules/@stdlib/utils/delay/docs/repl.txt
Planeshifter May 29, 2023
041fea8
Update lib/node_modules/@stdlib/utils/delay/docs/types/index.d.ts
Planeshifter May 29, 2023
c06e0aa
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Dec 10, 2023
3ec6a90
fix: address feedback from code review
Planeshifter Dec 10, 2023
a70cb93
test: comment out tests for now
Planeshifter Dec 10, 2023
9625289
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Dec 10, 2023
aa562df
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Feb 24, 2024
0d927c5
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Feb 24, 2024
19fa81a
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Feb 24, 2024
44fafb5
chore: update copyright years
stdlib-bot Feb 24, 2024
f494d7f
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Feb 28, 2024
ddd0741
Merge branch 'develop' into utils/delay
stdlib-bot Nov 16, 2024
a2eae7e
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Nov 17, 2024
1e84184
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Nov 17, 2024
3ad1b47
Merge remote-tracking branch 'upstream/develop' into utils/delay
stdlib-bot Jan 15, 2025
8faa10d
chore: format code in delay examples
Planeshifter Jan 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions lib/node_modules/@stdlib/utils/delay/README.md
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] )

Invokes a function after a specified number of milliseconds.

```javascript
function beep() {
console.log( 'beep' );
}

delay( beep, 3000 );
// Logs 'beep' after three seconds...
```

The function accepts additional arguments to invoke the function with.

```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...
```

The function returns a `timeout` object with the following properties and methods:

#### 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.

```javascript
function beep() {
console.log( 'beep' );
}
var t = delay( beep, 3000 );
t.invoke();
// Logs 'beep' immediately...
```

#### timeout.duration

Timeout duration (in milliseconds) before invoking a function.

```javascript
function beep() {
console.log( 'beep' );
}
var t = delay( beep, 3000 );
var duration = t.duration;
// 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.

```javascript
function beep() {
console.log( 'beep' );
}
var t = delay( beep, 3000 );
var status = t.status;
// returns 0

t.clear();
status = t.status;
// returns 2
```

</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.
- 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.

</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();

// 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">

</section>

<!-- /.links -->