pagmoのtimer関数
javascriptでtimerを濫立させると、firefoxで処理落ちが発生することがあります
その問題はtimerはひとつにまとめると解決しました
(解決したというより、処理は遅くなるが、処理落ちは発生しにくくなりました)
PAGMOでは以下のように書いたものを使っています。
良かったらどうぞ。
//// Function拡張 //// Function.prototype._inherits_ = function(parent){ this.prototype = new parent(); return this; }; Function.prototype._swiss_ = function(parent){ for(var i = 1;i<arguments.length;i++){ var name = arguments[i]; this.prototype[name] = parent.prototype[name]; } return this; }; Function.prototype._addition_ = function(funcs){ for(var i in funcs) this.prototype[i] = funcs[i]; return this; }; Function.prototype._new_ = function(){return new this()}; //// timerプロトタイプ //// TIMER = (function(){this.initialize.apply(this,arguments)})._addition_({ initialize: function(){ this.interval = false; this.orderArr = []; this.funcArr = []; }, start: function(){ var self = this; this.interval = setInterval(function(){self.timer()},30); }, set: function(type,func,time,id){ var time = Math.round(time/30); var time1,time2; if(!id) var id = 'none'; time1 = time2 = time; var pushArr = [func,time1,time2,id,type]; if(this.orderArr.length==0){ this.orderArr.push(pushArr); return this; } for(var i=0;i<this.orderArr.length;i++){ if(!this.orderArr[i]){this.orderArr[i] = pushArr; break;} else if(i == this.orderArr.length-1){this.orderArr.push(pushArr);break;} } return this; }, clear: function(id){ for(var i=0;i<this.orderArr.length;i++) if(!!this.orderArr[i]) for(var j=0;j<arguments.length;j++) if(this.orderArr[i][3]==arguments[j]) delete this.orderArr[i]; }, timer: function(){ for(var i=0;i<this.orderArr.length;i++){ if(!!this.orderArr[i]){ if(this.orderArr[i][1]<=0){ this.funcArr.push(this.orderArr[i][0]); if(this.orderArr[i][4]=='timeout') delete this.orderArr[i]; else if(this.orderArr[i][4]=='interval') this.orderArr[i][1] = this.orderArr[i][2]; } else this.orderArr[i][1] = eval(this.orderArr[i][1])-1; } } for(var i=0;i<this.funcArr.length;i++) this.funcArr[i](); this.funcArr = []; }, end: function(){ clearInterval(this.interval); } })._new_(); //// 使い方 //// var i = 0; TIMER.start(); //timerのスタート。 TIMER.set('interval',function(){alert(((i++)*3) + '秒');},5000,'func');//timerセット(ファンクション名,ms,id名) TIMER.clear('func');//timerクリア(id名) /* 「interval」となってる部分は、「timeout」にも出来ます。 「id」はクリアする予定の無いものは省略できます。 30msでタスクを監視して回っているので 30msで割り切れない値を突っ込むと30msに丸められます。 */