Permalink
Browse files

Summary: fixed up loops so they no longer lockup due to browser throt…

…tling.
  • Loading branch information...
Robert Hurst
Robert Hurst committed Mar 21, 2012
1 parent 45ee305 commit ddaa734d4b6dd08440c11c4222bbde63ea7c1906
Showing with 13 additions and 6 deletions.
  1. +13 −6 red-locomotive/loops.js
View
@@ -23,11 +23,15 @@ define(function() {
* Creates an event loop
* @param loopInterval
*/
- function EventLoop(loopInterval) {
+ function EventLoop(loopInterval, schedulerLimit) {
var emitter, active, lastTime, scheduledCycles;
//validate arguments
- if(loopInterval && typeof loopInterval !== 'number') { throw new Error('Event loop interval must be a number.'); }
+ if(loopInterval && typeof loopInterval !== 'number') { throw new Error('Cannot create event loop. If given the interval must be a number.'); }
+ if(schedulerLimit && typeof schedulerLimit !== 'number') { throw new Error('Cannot create event loop. If given the scheduler limit must be a number.'); }
+
+ loopInterval = loopInterval || 10;
+ schedulerLimit = schedulerLimit || 10;
emitter = engine.emitter();
active = false;
@@ -51,18 +55,20 @@ define(function() {
* @param time
*/
function loop(time) {
+ var batchedCycles;
if(active) {
//on first cycle set the last cycle time to the current cycle time
if(!lastTime) { lastTime = time; }
//add additional scheduled cycles if the expected frames is below ten
- if (scheduledCycles < 10) {
- scheduledCycles += (time - lastTime) / loopInterval;
- }
+ scheduledCycles += (time - lastTime) / loopInterval;
+
+ //cap the number of cycles
+ scheduledCycles > schedulerLimit && (scheduledCycles = schedulerLimit);
//get the number of expected cycles
- var batchedCycles = Math.floor(scheduledCycles);
+ batchedCycles = Math.floor(scheduledCycles);
//if there are cycles to be executed
if(batchedCycles > 0) {
@@ -74,6 +80,7 @@ define(function() {
//remove the batched cycles from the scheduled cycles
scheduledCycles -= batchedCycles;
+ console.log(batchedCycles);
//emit the "batch" event
emitter.trigger('cycle', time, batchedCycles);

0 comments on commit ddaa734

Please sign in to comment.