Skip to content

Commit f681d1a

Browse files
Add example for Timing methods (#1651)
* Add example and note in `k6/timers` * Fix note about experimental version removal * fix `eslint` conf * Port changes to `next` * Mention global availability in the experimental module note * Update docs/sources/next/shared/experimental-timers-module.md Co-authored-by: Heitor Tashiro Sergent <[email protected]> * Update docs/sources/next/shared/experimental-timers-module.md Co-authored-by: Heitor Tashiro Sergent <[email protected]> * Update docs/sources/next/javascript-api/k6-timers/_index.md Co-authored-by: Heitor Tashiro Sergent <[email protected]> --------- Co-authored-by: Heitor Tashiro Sergent <[email protected]>
1 parent d83df12 commit f681d1a

File tree

5 files changed

+70
-18
lines changed

5 files changed

+70
-18
lines changed

docs/sources/.eslintrc.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,9 @@ module.exports = {
4141
__ITER: 'readonly',
4242
open: 'readonly',
4343
window: 'readonly',
44+
setInterval: 'readonly',
45+
clearInterval: 'readonly',
46+
setTimeout: 'readonly',
47+
clearTimeout: 'readonly',
4448
},
4549
};
Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
11
---
2-
title: "k6/timers"
3-
description: "k6 timers API"
2+
title: 'k6/timers'
3+
description: 'k6 timers API'
44
weight: 11
55
---
66

77
# k6/timers
88

99
Implement timers to work with k6's event loop. They mimic the functionality found in browsers and other JavaScript runtimes.
1010

11-
| Function | Description |
12-
| :------------------------------------------ | :--------------------------------------------------------------------------------------------- |
13-
| [setTimeout](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) | Sets a function to be run after a given timeout. |
14-
| [clearTimeout](https://developer.mozilla.org/en-US/docs/Web/API/clearTimeout) | Clears a previously set timeout with `setTimeout`. |
15-
| [setInterval](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) | Sets a function to be run on a given interval. |
11+
| Function | Description |
12+
| :---------------------------------------------------------------------------- | :--------------------------------------------------- |
13+
| [setTimeout](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) | Sets a function to be run after a given timeout. |
14+
| [clearTimeout](https://developer.mozilla.org/en-US/docs/Web/API/clearTimeout) | Clears a previously set timeout with `setTimeout`. |
15+
| [setInterval](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) | Sets a function to be run on a given interval. |
1616
| [clearInterval](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) | Clears a previously set interval with `setInterval`. |
17+
18+
{{% admonition type="note" %}}
19+
20+
The timer methods are available globally, so you can use them in your script without including an import statement.
21+
22+
{{% /admonition %}}
23+
24+
## Example
25+
26+
```javascript
27+
export default function () {
28+
const intervalId = setInterval(() => {
29+
console.log('This runs every 200ms');
30+
}, 200);
31+
32+
const timeoutId = setTimeout(() => {
33+
console.log('This runs after 2s');
34+
35+
// clear the timeout and interval to exit k6
36+
clearInterval(intervalId);
37+
clearTimeout(timeoutId);
38+
}, 2000);
39+
}
40+
```

docs/sources/next/shared/experimental-timers-module.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ title: Experimental timers module admonition
44

55
{{% admonition type="caution" %}}
66

7-
Starting on k6 `v0.50`, the experimental module `k6/experimental/timers` has been graduated, and its functionality is now available in the [`k6/timers` module](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-timers/). The `k6/experimental/timers` is deprecated and will be removed in `v0.52.0`.
7+
The experimental module `k6/experimental/timers` has graduated, and its functionality is now available globally and through the [`k6/timers` module](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-timers/). The `k6/experimental/timers` is deprecated and will be removed.
88

9-
To migrate your scripts, replace all `k6/experimental/timers` imports with `k6/timers`.
9+
To migrate your scripts, remove the `k6/experimental/timers` imports.
1010

1111
{{% /admonition %}}
Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
11
---
2-
title: "k6/timers"
3-
description: "k6 timers API"
2+
title: 'k6/timers'
3+
description: 'k6 timers API'
44
weight: 11
55
---
66

77
# k6/timers
88

99
Implement timers to work with k6's event loop. They mimic the functionality found in browsers and other JavaScript runtimes.
1010

11-
| Function | Description |
12-
| :------------------------------------------ | :--------------------------------------------------------------------------------------------- |
13-
| [setTimeout](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) | Sets a function to be run after a given timeout. |
14-
| [clearTimeout](https://developer.mozilla.org/en-US/docs/Web/API/clearTimeout) | Clears a previously set timeout with `setTimeout`. |
15-
| [setInterval](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) | Sets a function to be run on a given interval. |
11+
| Function | Description |
12+
| :---------------------------------------------------------------------------- | :--------------------------------------------------- |
13+
| [setTimeout](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) | Sets a function to be run after a given timeout. |
14+
| [clearTimeout](https://developer.mozilla.org/en-US/docs/Web/API/clearTimeout) | Clears a previously set timeout with `setTimeout`. |
15+
| [setInterval](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) | Sets a function to be run on a given interval. |
1616
| [clearInterval](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) | Clears a previously set interval with `setInterval`. |
17+
18+
{{% admonition type="note" %}}
19+
20+
The timing methods are available globally, like in other JavaScript runtimes, and it is unnecessary to import them from the `k6/timers` module.
21+
22+
{{% /admonition %}}
23+
24+
## Example
25+
26+
```javascript
27+
export default function () {
28+
const intervalId = setInterval(() => {
29+
console.log('This runs every 200ms');
30+
}, 200);
31+
32+
const timeoutId = setTimeout(() => {
33+
console.log('This runs after 2s');
34+
35+
// clear the timeout and interval to exit k6
36+
clearInterval(intervalId);
37+
clearTimeout(timeoutId);
38+
}, 2000);
39+
}
40+
```

docs/sources/v0.52.x/shared/experimental-timers-module.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ title: Experimental timers module admonition
44

55
{{% admonition type="caution" %}}
66

7-
Starting on k6 `v0.50`, the experimental module `k6/experimental/timers` has been graduated, and its functionality is now available in the [`k6/timers` module](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-timers/). The `k6/experimental/timers` is deprecated and will be removed in `v0.52.0`.
7+
The experimental module `k6/experimental/timers` has graduated, and its functionality is now globally available and through the [`k6/timers` module](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-timers/). The `k6/experimental/timers` is deprecated and will be removed.
88

9-
To migrate your scripts, replace all `k6/experimental/timers` imports with `k6/timers`.
9+
To migrate your scripts, remove the `k6/experimental/timers` imports or import them using the `k6/timers` module.
1010

1111
{{% /admonition %}}

0 commit comments

Comments
 (0)