How to create an accurate timer in javascript?

前端 未结 10 2314
一个人的身影
一个人的身影 2020-11-22 01:32

I need to create a simple but accurate timer.

This is my code:

var seconds = 0;
setInterval(function() {
timer.innerHTML = seconds++;
}, 1000);
         


        
10条回答
  •  旧时难觅i
    2020-11-22 02:16

    One of my simplest implementations is down below. It can even survive page reloads. :-

    Code pen: https://codepen.io/shivabhusal/pen/abvmgaV

    $(function() {
      var TTimer = {
        startedTime: new Date(),
        restoredFromSession: false,
        started: false,
        minutes: 0,
        seconds: 0,
        
        tick: function tick() {
          // Since setInterval is not reliable in inactive windows/tabs we are using date diff.
          var diffInSeconds = Math.floor((new Date() - this.startedTime) / 1000);
          this.minutes = Math.floor(diffInSeconds / 60);
          this.seconds = diffInSeconds - this.minutes * 60;
          this.render();
          this.updateSession();
        },
        
        utilities: {
          pad: function pad(number) {
            return number < 10 ? '0' + number : number;
          }
        },
        
        container: function container() {
          return $(document);
        },
        
        render: function render() {
          this.container().find('#timer-minutes').text(this.utilities.pad(this.minutes));
          this.container().find('#timer-seconds').text(this.utilities.pad(this.seconds));
    
        },
        
        updateSession: function updateSession() {
          sessionStorage.setItem('timerStartedTime', this.startedTime);
        },
        
        clearSession: function clearSession() {
          sessionStorage.removeItem('timerStartedTime');
        },
        
        restoreFromSession: function restoreFromSession() {
          // Using sessionsStorage to make the timer persistent
          if (typeof Storage == "undefined") {
            console.log('No sessionStorage Support');
            return;
          }
    
          if (sessionStorage.getItem('timerStartedTime') !== null) {
            this.restoredFromSession = true;
            this.startedTime = new Date(sessionStorage.getItem('timerStartedTime'));
          }
        },
        
        start: function start() {
          this.restoreFromSession();
          this.stop();
          this.started = true;
          this.tick();
          this.timerId = setInterval(this.tick.bind(this), 1000);
        },
        
        stop: function stop() {
          this.started = false;
          clearInterval(this.timerId);
          this.render();
        }
      };
    
      TTimer.start();
    
    });
    
    
    

    00 : 00

提交回复
热议问题