Skip to content
Buy me a coffee

TimeSpan

TimeSpan is an immutable interval stored in milliseconds. Every API that takes a delay (abortAfter, timeout, EnhancedAbortSignal.timeout) accepts a number of milliseconds or a TimeSpan.

const
const controller: EnhancedAbortController
controller
= new
new EnhancedAbortController(): EnhancedAbortController

An AbortController with .NET CancellationTokenSource features. It is an AbortController, and controller.signal is a real AbortSignal: pass it to fetch, AbortSignal.any, streams, timers and any other API that takes a signal.

@example

const controller = new EnhancedAbortController().abortAfter(TimeSpan.fromSeconds(5));
const response = await fetch(url, { signal: controller.signal });

EnhancedAbortController
().
EnhancedAbortController.abortAfter(delay: Delay): EnhancedAbortController

Aborts after delay, like .NET's CancelAfter: calling it again replaces the previous schedule, and Infinity / -1 / TimeSpan.infinite cancel it. The reason is a DOMException named TimeoutError, as with AbortSignal.timeout. The timer never keeps the process alive, and delays longer than 24.8 days work. Does nothing once aborted. Returns the controller, for chaining.

@throws ― RangeError for NaN, negative (other than -1) or non-numeric delays.

@throws ― Error if the controller was disposed.

abortAfter
(
class TimeSpan

An immutable time interval, stored in milliseconds. Modeled on .NET's TimeSpan.

@example

controller.abortAfter(TimeSpan.fromSeconds(30));
TimeSpan.fromMinutes(1).add(TimeSpan.fromSeconds(30)).totalSeconds; // 90

TimeSpan
.
TimeSpan.fromSeconds(seconds: number): TimeSpan
fromSeconds
(30));
const
const span: TimeSpan
span
=
class TimeSpan

An immutable time interval, stored in milliseconds. Modeled on .NET's TimeSpan.

@example

controller.abortAfter(TimeSpan.fromSeconds(30));
TimeSpan.fromMinutes(1).add(TimeSpan.fromSeconds(30)).totalSeconds; // 90

TimeSpan
.
TimeSpan.fromMinutes(minutes: number): TimeSpan
fromMinutes
(1).
TimeSpan.add(other: TimeSpan): TimeSpan

A new interval: this one plus other.

add
(
class TimeSpan

An immutable time interval, stored in milliseconds. Modeled on .NET's TimeSpan.

@example

controller.abortAfter(TimeSpan.fromSeconds(30));
TimeSpan.fromMinutes(1).add(TimeSpan.fromSeconds(30)).totalSeconds; // 90

TimeSpan
.
TimeSpan.fromSeconds(seconds: number): TimeSpan
fromSeconds
(30));
const span: TimeSpan
span
.
TimeSpan.totalSeconds: number

The interval in whole and fractional seconds.

totalSeconds
; // 90
const span: TimeSpan
span
.
TimeSpan.milliseconds: number

The interval in milliseconds.

milliseconds
; // 90000
var String: StringConstructor
(value?: any) => string

Allows manipulation and formatting of text strings and determination and location of substrings within strings.

String
(
const span: TimeSpan
span
); // '90000ms'
+
const span: TimeSpan
span
; // 90000 (valueOf)
const span: TimeSpan
span
>
class TimeSpan

An immutable time interval, stored in milliseconds. Modeled on .NET's TimeSpan.

@example

controller.abortAfter(TimeSpan.fromSeconds(30));
TimeSpan.fromMinutes(1).add(TimeSpan.fromSeconds(30)).totalSeconds; // 90

TimeSpan
.
TimeSpan.fromMinutes(minutes: number): TimeSpan
fromMinutes
(1); // true
Create
new TimeSpan(ms), TimeSpan.fromMilliseconds(ms)
fromSeconds, fromMinutes, fromHours, fromDays Fractions allowed: fromHours(1.5)
Read
milliseconds The stored value
totalSeconds, totalMinutes, totalHours, totalDays Whole and fractional
Combine and compare
add(other), subtract(other) New TimeSpans
equals(other) Same length
compareTo(other) -1, 0 or 1
valueOf() Milliseconds, so <, > and +span work
toString() "1500ms"
Constants
TimeSpan.zero 0 ms
TimeSpan.infinite -1 ms, .NET’s Timeout.InfiniteTimeSpan: abortAfter(TimeSpan.infinite) cancels a deadline
TimeSpan.maxValue, TimeSpan.minValue ±Number.MAX_SAFE_INTEGER ms (about 285,616 years)

Every factory throws a RangeError for NaN and ±Infinity: TimeSpan.fromSeconds(NaN) fails where it is written, not later as an instant abort.

TimeSpan itself allows negatives (for arithmetic), but a delay must be non-negative: abortAfter(TimeSpan.fromSeconds(-5)) throws a RangeError. The one exception is -1 ms (TimeSpan.infinite), which means “no deadline”.