Skip to content

Instantly share code, notes, and snippets.

@x9t9
Forked from fundon/timer.js
Created May 11, 2019 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x9t9/bf2234e29b30af776407bd39dede32d0 to your computer and use it in GitHub Desktop.
Save x9t9/bf2234e29b30af776407bd39dede32d0 to your computer and use it in GitHub Desktop.
One Page One Timer
var Timer = {
i: 0,
t: 0,
queue: {},
tq: {},
add: function (time, callback) {
if (!this.queue[time]) {
this.queue[time] = [];
this.tq[time] = 0;
}
this.queue[time].push(callback);
},
_timer: null,
run: function () {
var self = this, q = self.queue;
self._timer = setInterval(function () {
self.i++;
self.t += 20;
for (var k in q) {
console.log(self.t);
if (self.t % k === 0) {
for (var j = 0; j < q[k].length; j++) {
(function(a) {
setTimeout(function () {
a();
}, 0);
})(q[k][j]);
}
}
}
if (self.i > 100) {
clearInterval(self._timer);
}
}, 20);
}
};
Timer.add(160, function () {
console.log('160ss start ...');
});
Timer.add(100, function () {
console.log('100ss start ...');
});
Timer.add(1000, function () {
console.log('1ms start ...');
});
/*
Timer.add(2000, function () {
console.log('2ms do ...');
});
Timer.add(3000, function () {
console.log('3ms do ...');
});
*/
Timer.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment