Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                # SpeedClock class

## constructor

```js
const clock = new SpeedClock();

// or chain it:
const runningClock = new SpeedClock().start();
```

## start

Starts the clock. 

<strong>returns:</strong> instance (so it is chainable)

```js
const speedFactor = 1; /* default value */
clock.start(speedFactor);

// if no speedFactor is specified, 
// it takes the current speed factor
// the speedFactor is initially 1
clock.start();
```

## stop

Stops the clock

<strong>returns:</strong> instance

```js
clock.stop();
```

## reset

Resets the clock to zero. Doesn't change the speedFactor.

<strong>returns:</strong> instance

```js
clock.reset();
```

<strong>returns:</strong> instance

## running

Returns whether the clock is running

<strong>type:</strong> boolean, readonly

```
const running = clock.running;
```

## elapsedTime 

<strong>type:</strong> number, readonly

```
const elapsed = clock.elapsedTime
```

## speedFactor 

<strong>type:</strong> number

Get or set the speed factor, also while the clock is running.

```
// run the clock at 4x speed.
clock.speedFactor = 4
```

## Differences from `THREE.Clock()`

- The clock is initialized in stopped state.
- Uses getters. So it's `clock.elapsedTime` rather than `threeClock.getElapsedTime()` 


              
            
!

CSS

              
                body {
  background: #111;
  color: #fff;
  font-family: sans-serif;
  font-size: 1.25rem;
  line-height: 1.4;
}

h1 { color: deepskyblue; }
h2, h3 { color: mediumseagreen; }

pre {
  background: #000;
  padding: 1rem;
  color: #cfc;
  border-radius: .25rem;
}

              
            
!

JS

              
                class SpeedClock {

  constructor() {
    this._startTime = NaN;
    this._totalElapsed = 0;
    this._running = false;
    this._speedFactor = 1;
  }

  /**
   * start the stopwatch
   * @returns {SpeedClock} instance
   */
  start(speedFactor) {
    if (this._running) {
      this.stop();
    }
    if (typeof speedFactor !== "undefined") {
      this._speedFactor = speedFactor;  
    }
    this._startTime = performance.now();
    this._running = true;
    return this;
  }

  /**
   * stop the stopwatch
   * @returns instance
   */
  stop() {
    if (this._running && !Number.isNaN(this._startTime)) {
      this._running = false;
      this._totalElapsed += (performance.now() - this._startTime) * this._speedFactor * 1e-3;
    }
    return this;
  }

  /**
   * reset the stopwatch
   * @returns instance
   */
  reset() {
    this._totalElapsed = 0;
    this._startTime = this._running ? performance.now() : NaN;
    return this;
  }

  /**
   * get run status
   * @returns {boolean} true if running
   */
  get running() {
    return this._running;
  }

  /**
   * get elapsed time
   * @returns {number} elapsed time
   */
  get elapsedTime() {
    return this._running
      ? this._totalElapsed + (performance.now() - this._startTime) * this._speedFactor * 1e-3
      : this._totalElapsed;
  }
  
  /**
   * get speed factor
   * @returns {number} speed factor
   */
  get speedFactor() {
    return this._speedFactor;
  }
  
  /**
   * set speed factor
   */
  set speedFactor(value) {
    this.stop().start(value);
  }
}
              
            
!
999px

Console