A utility class representing a counter that can be incremented and reset.
The Counter class provides a simple mechanism to maintain an incremental state, beginning from an optional initial value (defaults to 0).
Basic usage showing initialization, incrementing, and resetting:
const counter = new Counter(10);console.log(counter.current); // 10counter.next(); // 11console.log(counter.current); // 11counter.reset();console.log(counter.current); // 10 Copy
const counter = new Counter(10);console.log(counter.current); // 10counter.next(); // 11console.log(counter.current); // 11counter.reset();console.log(counter.current); // 10
Initializes a new instance of the Counter class.
The starting value of the counter. Defaults to 0.
Gets the current value of the counter.
Increments the counter and returns the new value.
The updated counter value.
Resets the counter back to its initial value.
A utility class representing a counter that can be incremented and reset.
The Counter class provides a simple mechanism to maintain an incremental state, beginning from an optional initial value (defaults to 0).
Example
Basic usage showing initialization, incrementing, and resetting: