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

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

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

PREVIOUS NEXT
Code Example
Javascript :: json generator 
Javascript :: web application development software 
Javascript :: Array#splice 
Javascript :: convert number into string 
Javascript :: rimraf node.js 
Javascript :: react native better camera 
Javascript :: web app let user confirm closing tab 
Javascript :: mongodb mongoose concatenate two values before get 
Javascript :: react native store sensitive data in redux 
Javascript :: mock js random 
Javascript :: crud application in mean stack 
Javascript :: import css files maven resources with jsf 
Javascript :: how to firebase.database().ref push unique id in same unique id firebase 
Javascript :: unslick if more then 
Javascript :: convert css box shadow to react native 
Javascript :: is javascript case sensitive 
Javascript :: bcrypt always return faslse in node js 
Javascript :: document.elementFromPoint 
Javascript :: how to program in javascript and jquery on a page 
Javascript :: await zoomus.joinmeeting crashing app react native 
Javascript :: create pair foreach item in array 
Javascript :: string and charater alphabet order 
Javascript :: Map the peoples of Ray such as their first name comes first in the string in js 
Javascript :: local = 1 
Javascript :: selected css based on route react 
Javascript :: rfc 7230 
Javascript :: reflection of an graph javascript 
Javascript :: How to get anchor tag value using tag id 
Javascript :: how to pass a variable to jspf 
Javascript :: javascript factor chain 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =