Created
December 16, 2011 00:57
-
-
Save sixthgear/1483856 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 events = []; | |
| var intervals = []; | |
| var max_id = 0; | |
| function setInterval(callback, period) { | |
| intervals.push(new Interval(callback, period)); | |
| return ++max_id; | |
| } | |
| function clearInterval(id) { | |
| intervals.splice(id, 1); | |
| } | |
| function eventLoop() { | |
| var dt; | |
| var this_time; | |
| var prev_time; | |
| while (true) { | |
| // calculate dt | |
| this_time = Date.getTime(); | |
| dt = this_time - prev_time; | |
| prev_time = this_time; | |
| // process event stack | |
| while(events) { | |
| var e = events.pop(); | |
| e.callback(e.event); | |
| } | |
| // process timeouts and intervals | |
| for (i in intervals) { | |
| intervals[i].accumulator -= dt; | |
| if (intervals[i].accumulator <= 0) { | |
| intervals[i].callback(); | |
| intervals[i].accumulator += intervals[i].period; | |
| } | |
| } | |
| } | |
| } | |
| eventLoop(); |
sixthgear
commented
Dec 16, 2011
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment