timers.js revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5timers = new (function() {
6
7this.timers_ = {};
8
9this.start = function(name, callback, intervalSeconds) {
10  this.stop(name);
11
12  var timerId = setInterval(callback, intervalSeconds * 1000);
13
14  this.timers_[name] = {
15    name: name,
16    callback: callback,
17    timerId: timerId,
18    intervalSeconds: intervalSeconds
19  };
20
21  callback();
22};
23
24this.stop = function(name) {
25  if (name in this.timers_) {
26    clearInterval(this.timers_[name].timerId);
27    delete this.timers_[name];
28  }
29};
30
31this.stopAll = function() {
32  for (var name in this.timers_) {
33    clearInterval(this.timers_[name].timerId);
34  }
35  this.timers_ = {};
36};
37
38})();