# Rx.VirtualTimeScheduler

Base class for providing scheduling in virtual time. This inherits from the `Rx.Scheduler` class.

## Usage #\#

The following shows an example of using the `Rx.VirtualTimeScheduler`. In order for this to work, you must implement the `add`, `toDateTimeOffset` and `toRelative` methods as described below.

```javascript
/* Comparer required for scheduling priority */
function comparer (x, y) {
    if (x > y) { return 1; }
    if (x < y) { return -1; }
    return 0;
}

var scheduler = new Rx.VirtualTimeScheduler(0, comparer);

/**
 * Adds a relative time value to an absolute time value.
 * @param {Any} absolute Absolute virtual time value.
 * @param {Any} relative Relative virtual time value to add.
 * @return {Any} Resulting absolute virtual time sum value.
 */
scheduler.add = function (absolute, relative) {
    return absolute + relative;
};

/**
 * Converts an absolute time to a number
 * @param {Number} The absolute time in ms
 * @returns {Number} The absolute time in ms
 */
scheduler.toDateTimeOffset = function (absolute) {
    return new Date(absolute).getTime();
};

/**
 * Converts the time span number/Date to a relative virtual time value.
 * @param {Number} timeSpan TimeSpan value to convert.
 * @return {Number} Corresponding relative virtual time value.
 */
scheduler.toRelative = function (timeSpan) {
    return timeSpan;
};

// Schedule some time
scheduler.scheduleAbsolute(1, function () { console.log('foo'); });
scheduler.scheduleAbsolute(2, function () { console.log('bar'); });
scheduler.scheduleAbsolute(3, function () { scheduler.stop(); });

// Start the scheduler
scheduler.start();

// => foo
// => bar

// Check the clock once stopped
console.log(scheduler.now());
// => 3

console.log(scheduler.clock);
// => 3
```

### Location

* rx.virtualtime.js

## `VirtualTimeScheduler Constructor` #\#

* [`constructor`](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerinitialclock-comparer)

## `VirtualTimeScheduler Instance Methods` #\#

* [`advanceBy`](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypeadvancebytime)
* [`advanceTo`](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypeadvancetotime)
* [`scheduleAbsolute`](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypescheduleabsoluteduetime-action)
* [`scheduleAbsoluteWithState`](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypescheduleabsolutewithstatestate-duetime-action)
* [`scheduleRelative`](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypeschedulerelativeduetime-action)
* [`scheduleRelativeWithState`](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypeschedulerelativewithstatestate-duetime-action)
* [`sleep`](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypesleeptime)
* [`start`](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvritualtimeschedulerprototypestart)
* [`stop`](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvritualtimeschedulerprototypestop)

## `VirtualTimeScheduler Instance Properties` #\#

* [`isEnabled`](/rxjs-book/summary/schedulers/virtual_scheduler.md#isenabled)

## `VirtualTimeScheduler Protected Abstract Methods` #\#

* [`add`](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypeaddabsolute-relative)
* [`toDateTimeOffset`](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypetodatetimeoffsetabsolute)
* [`toRelative`](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypetorelativetimespan)

## `VirtualTimeScheduler Protected Methods` #\#

* [`getNext`](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypegetnext)

## Inherited Classes #\#

* [`Rx.Scheduler`](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/schedulers/scheduler.md)

## *VirtualTimeScheduler Constructor* #\#

### `Rx.VirtualTimeScheduler(initialClock, comparer)` <a href="#rxvirtualtimescheduler" id="rxvirtualtimescheduler"></a>

[#](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimescheduler) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js#L38-L44)

Creates a new virtual time scheduler with the specified initial clock value and absolute time comparer.

#### Arguments

1. `initialClock` *(Function)*: Initial value for the clock.
2. `comparer` *(Function)*: Comparer to determine causality of events based on absolute time.

#### Example

```javascript
function comparer (x, y) {
    if (x > y) { return 1; }
    if (x < y) { return -1; }
    return 0;
}

var scheduler = new Rx.VirtualTimeScheduler(
    0,          /* initial clock of 0 */
    comparer    /* comparer for determining order */
);
```

### Location

* rx.virtualtime.js

## *VirtualTimeScheduler Instance Methods* #\#

### `Rx.VirtualTimeScheduler.prototype.advanceBy(time)` <a href="#rxvirtualtimeschedulerprototypeadvancebytime" id="rxvirtualtimeschedulerprototypeadvancebytime"></a>

[#](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypeadvancebytime) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js#L166-L176)

Advances the scheduler's clock by the specified relative time, running all work scheduled for that timespan.

#### Arguments

1. `time` *(Any)*: Relative time to advance the scheduler's clock by.

#### Example

```javascript
var scheduler = new MyVirtualScheduler(
    200 /* initial time */
);

scheduler.scheduleAbsolute(250, function () {
    console.log('hello');
});

scheduler.advanceBy(300);
// => hello

console.log(scheduler.clock);
// => 500
```

### Location

* rx.virtualtime.js

### `Rx.VirtualTimeScheduler.prototype.advanceTo(time)` <a href="#rxvirtualtimeschedulerprototypeadvancetotime" id="rxvirtualtimeschedulerprototypeadvancetotime"></a>

[#](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypeadvancetotime) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js#L136-L160)

Advances the scheduler's clock to the specified time, running all work till that point.

#### Arguments

1. `time` *(Any)*: Absolute time to advance the scheduler's clock to.

#### Example

```javascript
var scheduler = new MyVirtualScheduler(
    0 /* initial time */
);

scheduler.scheduleAbsolute(100, function () {
    console.log('hello');
});

scheduler.scheduleAbsolute(200, function () {
    console.log('world');
});

scheduler.advanceBy(300);
// => hello
// => world

console.log(scheduler.clock);
// => 300
```

### Location

* rx.virtualtime.js

### `Rx.VirtualTimeScheduler.prototype.scheduleAbsolute(dueTime, action)` <a href="#rxvirtualtimeschedulerprototypescheduleabsoluteduetime-action" id="rxvirtualtimeschedulerprototypescheduleabsoluteduetime-action"></a>

[#](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypescheduleabsoluteduetime-action) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js#L216-L218)

Schedules an action to be executed at dueTime.

#### Arguments

1. `dueTime` *(Any)*: Absolute time at which to execute the action.
2. `action` *(Function)*: Action to be executed.

#### Returns

*(Disposable)*: The disposable object used to cancel the scheduled action (best effort).

#### Example

```javascript
var scheduler = new MyVirtualScheduler(
    0 /* initial time */
);

scheduler.scheduleAbsolute(100, function () {
    console.log('hello');
});

scheduler.scheduleAbsolute(200, function () {
    console.log('world');
});

scheduler.advanceBy(300);
// => hello
// => world

console.log(scheduler.clock);
// => 300
```

### Location

* rx.virtualtime.js

### `Rx.VirtualTimeScheduler.prototype.scheduleAbsoluteWithState(state, dueTime, action)` <a href="#rxvirtualtimeschedulerprototypescheduleabsolutewithstatestate-duetime-action" id="rxvirtualtimeschedulerprototypescheduleabsolutewithstatestate-duetime-action"></a>

[#](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypescheduleabsolutewithstatestate-duetime-action) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js#L227-L236)

Schedules an action to be executed at dueTime.

#### Arguments

1. `state`: *(Any)*: State passed to the action to be executed.
2. `dueTime` *(Any)*: Absolute time at which to execute the action.
3. `action` *(Function)*: Action to be executed.

#### Returns

*(Disposable)*: The disposable object used to cancel the scheduled action (best effort).

#### Example

```javascript
var scheduler = new MyVirtualScheduler(
    0 /* initial time */
);

scheduler.scheduleAbsoluteWithState('world', 100, function (x) {
    console.log('hello ' + x);
});

scheduler.scheduleAbsoluteWithState(200, function () {
    console.log('goodnight ' + x);
}, 'moon');

scheduler.start();
// => hello world
// => goodnight moon

console.log(scheduler.clock);
// => 200
```

### Location

* rx.virtualtime.js

### `Rx.VirtualTimeScheduler.prototype.scheduleRelative(dueTime, action)` <a href="#rxvirtualtimeschedulerprototypeschedulerelativeduetime-action" id="rxvirtualtimeschedulerprototypeschedulerelativeduetime-action"></a>

[#](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypeschedulerelativeduetime-action) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js#L100-L102)

Schedules an action to be executed at dueTime.

#### Arguments

1. `dueTime` *(Any)*: Relative time after which to execute the action.
2. `action` *(Function)*: Action to be executed.

#### Returns

*(Disposable)*: The disposable object used to cancel the scheduled action (best effort).

#### Example

```javascript
var scheduler = new MyVirtualScheduler(
    100 /* initial time */
);

scheduler.scheduleRelative(100, function () {
    console.log('hello');
});

scheduler.scheduleRelative(200, function () {
    console.log('world');
});

scheduler.start();
// => hello
// => world

console.log(scheduler.clock);
// => 400
```

### Location

* rx.virtualtime.js

### `Rx.VirtualTimeScheduler.prototype.scheduleRelativeWithState(state, dueTime, action)` <a href="#rxvirtualtimeschedulerprototypeschedulerelativewithstatestate-duetime-action" id="rxvirtualtimeschedulerprototypeschedulerelativewithstatestate-duetime-action"></a>

[#](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypeschedulerelativewithstatestate-duetime-action) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js#L89-L92)

Schedules an action to be executed at dueTime.

#### Arguments

1. `state`: *(Any)*: State passed to the action to be executed.
2. `dueTime` *(Any)*: Relative time after which to execute the action.
3. `action` *(Function)*: Action to be executed.

#### Returns

*(Disposable)*: The disposable object used to cancel the scheduled action (best effort).

#### Example

```javascript
var scheduler = new MyVirtualScheduler(
    0 /* initial time */
);

scheduler.scheduleRelativeWithState('world', 100, function (x) {
    console.log('hello ' + x);
});

scheduler.scheduleRelativeWithState('moon', 200, function () {
    console.log('goodnight ' + x);
});

scheduler.start();
// => hello world
// => goodnight moon

console.log(scheduler.clock);
// => 300
```

### Location

* rx.virtualtime.js

### `Rx.VirtualTimeScheduler.prototype.sleep(time)` <a href="#rxvritualtimeschedulerprototypesleeptime" id="rxvritualtimeschedulerprototypesleeptime"></a>

[#](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvritualtimeschedulerprototypesleeptime) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js#L182-L190)

Advances the scheduler's clock by the specified relative time.

#### Arguments

1. `time` *(Any)*: Relative time to advance the scheduler's clock by.

#### Example

```javascript
var scheduler = new MyVirtualScheduler(
    0 /* initial time */
);

scheduler.sleep(400);

console.log(scheduler.clock);
// => 400
```

### Location

* rx.virtualtime.js

### `Rx.VirtualTimeScheduler.prototype.start()` <a href="#rxvritualtimeschedulerprototypestart" id="rxvritualtimeschedulerprototypestart"></a>

[#](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvritualtimeschedulerprototypestart) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js#L107-L123)

Starts the virtual time scheduler.

#### Example

```javascript
var scheduler = new MyVirtualScheduler(
    0 /* initial time */
);

scheduler.scheduleRelativeWithState('world', 100, function (x) {
    console.log('hello ' + x);
});

scheduler.scheduleRelativeWithState('moon', 200, function () {
    console.log('goodnight ' + x);
});

scheduler.start();
// => hello world
// => goodnight moon

console.log(scheduler.clock);
// => 400
```

### Location

* rx.virtualtime.js

### `Rx.VirtualTimeScheduler.prototype.stop()` <a href="#rxvritualtimeschedulerprototypestop" id="rxvritualtimeschedulerprototypestop"></a>

[#](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvritualtimeschedulerprototypestop) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/concurrency/virtualtimescheduler.js#L128-L130)

Stops the virtual time scheduler.

#### Example

```javascript
var scheduler = new MyVirtualScheduler(
    0 /* initial time */
);

scheduler.scheduleRelative(100, function () {
    console.log('hello world');
});

scheduler.scheduleRelative(100, function () {
    scheduler.stop();
});

scheduler.scheduleRelative(100, function () {
    console.log('hello world');
});

scheduler.start();
// => hello world
```

### Location

* rx.virtualtime.js

## *VirtualTimeScheduler Abstract Protected Methods* #\#

### `Rx.VirtualTimeScheduler.prototype.add(absolute, relative)` <a href="#rxvirtualtimeschedulerprototypeaddabsolute-relative" id="rxvirtualtimeschedulerprototypeaddabsolute-relative"></a>

[#](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypeaddabsolute-relative) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/subjects/asyncsubject.js#L54)

Adds a relative time value to an absolute time value. This method is used in several methods including `scheduleRelativeWithState`, `advanceBy` and `sleep`.

### Arguments

1. `absolute` *(Any)*: Absolute virtual time value.
2. `relative` *(Any)*: Relative virtual time value.

#### Returns

*(Any)*: Resulting absolute virtual time sum value.

#### Example

One possible implementation could be as simple as the following:

```javascript
scheduler.add = function (absolute, relative) {
    return absolute + relative;
};
```

### Location

* rx.virtualtime.js

### `Rx.VirtualTimeScheduler.prototype.toDateTimeOffset(absolute)` <a href="#rxvirtualtimeschedulerprototypetodatetimeoffsetabsolute" id="rxvirtualtimeschedulerprototypetodatetimeoffsetabsolute"></a>

[#](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypetodatetimeoffsetabsolute) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/subjects/asyncsubject.js#L61)

Converts an absolute time to a number. This is used directly in the `now` method on the `Rx.Scheduler`

### Arguments

1. `absolute` *(Any)*: The absolute time to convert.

#### Returns

*(Number)*: The absolute time in ms.

#### Example

One possible implementation could be as simple as the following:

```javascript
// String -> Number
scheduler.toDateTimeOffset = function (absolute) {
    return return absolute.length;
};
```

### Location

* rx.virtualtime.js

### `Rx.VirtualTimeScheduler.prototype.toRelative(timeSpan)` <a href="#rxvirtualtimeschedulerprototypetorelativetimespan" id="rxvirtualtimeschedulerprototypetorelativetimespan"></a>

[#](/rxjs-book/summary/schedulers/virtual_scheduler.md#rxvirtualtimeschedulerprototypetorelativetimespan) [Ⓢ](https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/subjects/asyncsubject.js#L61)

Converts the time span number/Date to a relative virtual time value.

### Arguments

1. `timeSpan` *(Any)*: The time span number value to convert.  This is used directly in `scheduleWithRelativeAndState` and `scheduleWithAbsoluteAndState`.

#### Returns

*(Number)*: Corresponding relative virtual time value.

#### Example

One possible implementation could be as simple as the following:

```javascript
// Number -> Number
scheduler.toRelative = function (timeSpan) {
    return timeSpan;
};
```

### Location

* rx.virtualtime.js


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://soufatn.gitbook.io/rxjs-book/summary/schedulers/virtual_scheduler.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
