Timer Processing.js

I am developing an application using Processing.js.

At each step of the drawing cycle, I increase the number of frames by one frame++.

I want to know how much time has passed. Currently, to get the time (knowing that my application is configured to run on 60FPS), I like to: time=frame/60. But this only works if the application always works exactly with FPS, and we all know that this is not so, because it depends on the user's equipment.

I want the timer to be pretty accurate (only with error 0.0001s).

It is also recommended that you welcome some javascript algorithm to calculate the difference between now () and start_time ().

+5
source share
3 answers

.

, , . .

, , .

-

var startTimer = new Date(); // at start (once)

,

var passed = new Date() - startTimer; // in milliseconds

http://jsfiddle.net/gaby/CF4Ju/

+2

, . .

, Process.js frameCount. , .

+3

. millis() frameRate

class Timer{
  boolean increment, inProgress;
  int spawn, end;
  int seconds, tm_limit;

  Timer(int tm_limit){
    this.tm_limit = tm_limit;
    seconds = tm_limit;
    increment = false;
    inProgress = false;
  }

  Timer(){
    seconds = 0;
    increment = true;
    inProgress = false;
  }

  void start(){
    inProgress = true;
    spawn = millis();
  }

  void stop(){
    inProgress = false;
    end = millis();
  }

  int getSeconds(){
    if(inProgress){
      if(increment){
        seconds = int((millis() - spawn) / 1000);
      }
      else{
        if(seconds - int((millis() - spawn) / 1000) != seconds){
          seconds = seconds - int((millis() - spawn) / 1000);
          if(seconds <= 0) { stop(); }
          else spawn = millis();
        }
      }
    }
    return seconds;
  }

  void reset(){
    if(!increment)
      seconds = tm_limit;
    else
      seconds = 0;

    inProgress = false;
  }
}

If a Timer object is created with a parameter, it is assumed that the timer should decrease. Otherwise, the exit condition can be checked by receiving a value from the method getSeconds().

0
source

All Articles