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 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 :: == vs === javascript 
Javascript :: node js + mongoose 
Javascript :: class component params in react 
Javascript :: Expected an assignment or function call and instead saw an expression 
Javascript :: use ref in component reactjs 
Javascript :: express middleware status code always 200 
Javascript :: sumo multiselect 
Javascript :: giphy javascript github 
Javascript :: formatt json to create node and child node react 
Javascript :: redux if already exist item dont add to array 
Javascript :: delete class through the dom 
Javascript :: jquery like selector in javascript 
Javascript :: capacitorjs get zip code example 
Javascript :: alert on right click jquery 
Javascript :: all navigator CPU option in javascript 
Javascript :: select all child elements javascript 
Javascript :: popup react now ui 
Javascript :: javascript get the first day of the month and last day 
Javascript :: how to update a function to accept a name and have it displayed in JavaScript 
Javascript :: warn user before leaving page angular 
Javascript :: react prototype function 
Javascript :: how er create json data file to train our rasa model 
Javascript :: $("#symptomSelector").symptomSelector WHAT DOES THIS MEAN IN JAVASCRIPT 
Javascript :: what is the difference between angular changedetection default and onpush stratergy 
Javascript :: filtering array of friends javascript 
Javascript :: JS truthy value of void 
Javascript :: advantage of array 
Javascript :: scenario.getname() cucumber-js 
Javascript :: javascript path folder above 
Javascript :: eyeshot javascript version 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =