Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

timer in javascript

//single event i.e. alarm, time in milliseconds
var timeout = setTimeout(function(){yourFunction()},10000);
//repeated events, gap in milliseconds
var interval = setInterval(function(){yourFunction()},1000);
Comment

js 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

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

PREVIOUS NEXT
Code Example
Javascript :: chrome dev tools console api 
Javascript :: open source 
Javascript :: what does the useReducer do in react 
Javascript :: on hover event 
Javascript :: jquery val style 
Javascript :: angular get firebase firestore 
Javascript :: javascript factorial stack 
Javascript :: how to increase the window size in nightmare 
Javascript :: debounce polyfill 
Javascript :: js button to take current page screenshot 
Javascript :: react native tdd emzyme 
Javascript :: javascript nested objects 
Javascript :: Early return mdn 
Javascript :: _onResize vue leaflet 
Javascript :: run strapi plugin at startup 
Javascript :: london turnbridgewells 
Javascript :: npm ln 
Javascript :: js.l26 
Javascript :: convert to jsx 
Javascript :: special mc seed -131245679982 and 982652008272 April 23, 2021 
Javascript :: check if item is already registered in angular angularfire site:stackoverflow.com 
Javascript :: quokka create-react-app sample 
Javascript :: Uncaught Error: spawn node C:UsersLeonlDesktop pi-nano-serverelectronexpressserver.js ENOENT electron 
Javascript :: android studio react native plugins 
Javascript :: user agents regex for mobile 
Javascript :: test 
Javascript :: js replace blogger thumb 
Javascript :: json2csv parse with flatten example javascript 
Javascript :: shaynlink 
Javascript :: d3js circle out of scrren 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =