Created
September 9, 2011 10:24
-
-
Save fundon/1205903 to your computer and use it in GitHub Desktop.
One Page One Timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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