Skip to content

Instantly share code, notes, and snippets.

@nowri
Created March 16, 2015 11:18
Show Gist options
  • Select an option

  • Save nowri/7b24164cbc2927434b42 to your computer and use it in GitHub Desktop.

Select an option

Save nowri/7b24164cbc2927434b42 to your computer and use it in GitHub Desktop.
requestAnimationFrame対応タイマー
/* global define */
define({
init:function(func, args){
window.requestAnimationFrame = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
window.cancelAnimationFrame = window.cancelAnimationFrame ||
window.mozcancelAnimationFrame ||
window.webkitcancelAnimationFrame ||
window.mscancelAnimationFrame ||
function(id) {
clearTimeout(id);
};
this.func = func;
this.args = args || [];
},
play:function(){
this.reqId = window.requestAnimationFrame(this.loop.bind(this));
},
loop:function(){
if(this.func) {
this.func.apply(null, this.args);
}
this.reqId = window.requestAnimationFrame(this.loop.bind(this));
},
pause:function(){
if(this.reqId){
window.cancelAnimationFrame(this.reqId);
}
},
func:null
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment