Languages
[Edit]
EN

TypeScript - setInterval() function example

0 points
Created by:
maciek211
425

In this article, we would like to show you how to use setInterval() in TypeScript.

Quick solution:

or:

 

1. Documentation

Edit
Syntax

setInterval(callback)
setInterval(callback, delay)
setInterval(callback, delay, arg1, arg2, arg3, ...)

Parameterscallback - function called every indicated time (interval),
delay - optional interval time as an integer number (in milliseconds),
arg1, arg2, arg3, ... - optional arguments passed as callback arguments.
ResultCreated interval handle (as integer number).
Description

setInterval() function starts logic that calls callback function every indicated interval time.

setInterval() logic can be stopped by calling clearInterval() function with interval handle as an argument.

 

Below we present some practical examples for better understanding.

1. Simple setInterval example

Edit

In this example, we create and call a simple function that displays some text every second.

Runnable example:

2. Calling setInterval the specified amount of times

Edit

In this example, we create a function with setInterval that counts to 5 every second.

Runnable example:

3. Custom setInterval

Edit

In this example, we create customSetInterval that takes in three arguments:

  • count - how many times we want to call the callback function,
  • delay - time between callback function calls,
  • callback - a function to be called.

Runnable example:

Output (one line every second):

Alternative titles

  1. TypeScript - setInterval & clearInterval
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join