Giter VIP home page Giter VIP logo

timer.js's Introduction

Timer.js

A simple timer class with pause functionality.

Installation

npm install @ankr/timer

Usage

Timer comes with a simple api.

import { Timer } from "@ankr/timer";

// Start by creating a single Timer instance to be used everywhere.
const timer = new Timer();

// Start the timer before starting the game loop.
timer.start();

// Make sure to tick the timer inside your game loop.
timer.tick();

// Everywhere you use performance.now() or Date.now() use this instead.
timer.now();

// You can pause/resume/toggle the timer whenever you want.
timer.pause();
timer.resume();
timer.toggle();

// You know have access to delta time and total elapsed time through these properties
timer.deltaTime; // delta time in milliseconds
timer.elapsedTime; // total time in milliseconds
timer.deltaSeconds; // delta time in seconds
timer.elapsedSeconds; // total time in seconds

// There are also properties for checking if the timer is started and/or paused
timer.isStarted;
timer.isPaused;

Example

Here it is put together in a small example.

import { Timer } from "@ankr/timer";

const timer = new Timer();

const update = () => {
    // Update game logic using `timer.deltaTime`
};

const loop = () => {
    requestAnimationFrame(loop);
    timer.tick(); // Make sure to tick timer every frame
    update();
    render();
};

const start = () => {
    timer.start(); // Starting the timer
    loop();
};

start();

timer.js's People

Contributors

ankr avatar

Stargazers

Emil Møller avatar

Watchers

Kim Egede Jakobsen avatar  avatar Emil Møller avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.