Created
March 16, 2015 11:18
-
-
Save nowri/7b24164cbc2927434b42 to your computer and use it in GitHub Desktop.
requestAnimationFrame対応タイマー
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
| /* 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