2

I made a goog.Timer object (http://closure-library.googlecode.com/svn/docs/class_goog_Timer.html) with new goog.Timer(1) to run a function every millisecond via listening to the tick event. However, the function seemed to be running every 100 milliseconds instead.

I hypothesized that my function was taking a while to run (and javascript is single-threaded of course), so it took a while to get to the next round. Hence, I set the interval for the timer to 100, and it reliably ran every 1/10 of a second.

Does The Google Closure library have a more reliable timer that only runs a function precisely at the interval? If there is not enough time to run a function in one cycle, I am fine with canceling the previous call and running it the next time a tick is dispatched.

2
  • goog.Timer wraps the native setInterval, which itself is probably not fast enough to run your function in 1 ms. Are you sure you need that much granularity? 1ms polling is brutal. Can you not use goog.events.listen and dispatchEvent?
    – hyperslug
    Aug 5, 2012 at 5:15
  • 1
    Thanks, I don't need 1ms granularity. 20-100ms would be nice though. Aug 5, 2012 at 5:16

2 Answers 2

2

Nicholas Zakas wrote up a nice summary of Timer resolution in browsers on his blog. As Nicholas pointed out, the HTML5 timers specification (as of August 2, 2012) dictates that the minimum interval for setTimeout() and setInterval() is 4 milliseconds.

I wrote the following demo application to test the minimum interval delay for goog.Timer.

<!doctype html>
<html>
<head>
  <title>goog.Timer Test</title>
  <script src="../closure-library/closure/goog/base.js"></script>
</head>
<body>

<h1>goog.Timer Test</h1>

<div id="mainContent"></div>

<script>
  goog.require('goog.Timer');
</script>
<script>
  var tickCount = 0;
  var timer = new goog.Timer(1);
  var mainDiv = document.querySelector('#mainContent');

  /**
   * Tick callback.
   */
  var tickCounter = function() {
    tickCount++;
    if (tickCount % 1000 === 0) {
      var timeElapsed = goog.now() - startTime;
      mainDiv.innerHTML = 'goog.Timer tick events: ' + tickCount +
          '<br>actual elapsed milliseconds: ' + timeElapsed +
          '<br>milliseconds per goog.Timer tick: ' + timeElapsed/tickCount;
    }
  };

  startTime = goog.now();
  timer.start();
  goog.events.listen(timer, goog.Timer.TICK, tickCounter);
</script>
</body>
</html>

Running this program in Chrome version 21 consistently shows approximately 4.2 milliseconds per goog.Timer tick event, which is very close to the minimum allowable browser timer resolution of 4 milliseconds.

1

I dont know about goog timers, but this worked in my emulator. It stopped 3 milliseconds after I told it to though.

public Timer gameTimer;
int i = 1;
TextView TVsource;//You still have to assign this to a layout view in onCreate

public void runTimer() {

    gameTimer = new Timer();
    gameTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            if (i < 5000) {
                TimerMethod();
            }
        }
    }, 1000, 1);
}

public void TimerMethod() {
    this.runOnUiThread(Timer_Tick);
}

private Runnable Timer_Tick = new Runnable() {
    public void run() {
        i++;
        TVsource.setText("its " + i);
    }
};

P.S. I just realized this is a javascript question, but what the hell? Try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.