Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript timer

// great timer class, can start() stop() reset() and getTime()
class Timer {
  constructor () {
    this.isRunning = false;
    this.startTime = 0;
    this.overallTime = 0;
  }

  _getTimeElapsedSinceLastStart () {
    if (!this.startTime) {
      return 0;
    }
  
    return Date.now() - this.startTime;
  }

  start () {
    if (this.isRunning) {
      return console.error('Timer is already running');
    }

    this.isRunning = true;

    this.startTime = Date.now();
  }

  stop () {
    if (!this.isRunning) {
      return console.error('Timer is already stopped');
    }

    this.isRunning = false;

    this.overallTime = this.overallTime + this._getTimeElapsedSinceLastStart();
  }

  reset () {
    this.overallTime = 0;

    if (this.isRunning) {
      this.startTime = Date.now();
      return;
    }

    this.startTime = 0;
  }

  getTime () {
    if (!this.startTime) {
      return 0;
    }

    if (this.isRunning) {
      return this.overallTime + this._getTimeElapsedSinceLastStart();
    }

    return this.overallTime;
  }
}

const timer = new Timer();
timer.start();
setInterval(() => {
  const timeInSeconds = Math.round(timer.getTime() / 1000);
  document.getElementById('time').innerText = timeInSeconds;
}, 100)
Comment

javascript timer code

alert("dsadas");
Comment

javascript timer

// timer start
console.time('filter array');
const visibleTodos = getFilteredTodos(todos, filter);
// timer end
console.timeEnd('filter array');
Comment

PREVIOUS NEXT
Code Example
Javascript :: npx create-next-app permission denied 
Javascript :: check if element is displayed jsavascript 
Javascript :: create random salt js 
Javascript :: fselect jquery 
Javascript :: SHOPIFY CUSTOMER WITHOUT REGISTRATION 
Javascript :: javascript nested destructuring 
Javascript :: cara install parrot os di virtualbox 
Javascript :: fade animation vuetify js 
Javascript :: nuxtjs install sassloader 
Javascript :: how to plot a line only for current day pinescript 
Javascript :: Make React Tooltip work for dynamic elements 
Javascript :: blockchain.info/pushtx 
Javascript :: javascript ping ip address 
Javascript :: rechart graph 
Javascript :: bind() method #1 
Javascript :: jquery validate min and max value 
Javascript :: owl get parent state 
Javascript :: check if function exists 
Javascript :: volta node list 
Javascript :: how to create element with class in javascript 
Javascript :: Add Click events to multiple classes. 
Javascript :: proxy{} map in console 
Javascript :: apollo client multiple endpoints 
Javascript :: FirebaseError: Unknown field filter op. 
Javascript :: regex mobile 
Javascript :: Nested Components 
Javascript :: how to create session cookie in node js 
Javascript :: get 3 random items from array javascript 
Javascript :: shell curl path of json file as parameter without temporal file 
Javascript :: how to get value of selected radio button in javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =