Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var limitLoop = function (fn, fps) {
- // Use var then = Date.now(); if you
- // don't care about targetting < IE9
- var then = new Date().getTime();
- // custom fps, otherwise fallback to 60
- fps = fps || 60;
- var interval = 1000 / fps;
- return (function loop(time){
- requestAnimationFrame(loop);
- // again, Date.now() if it's available
- var now = new Date().getTime();
- var delta = now - then;
- if (delta > interval) {
- // Update time
- // now - (delta % interval) is an improvement over just
- // using then = now, which can end up lowering overall fps
- then = now - (delta % interval);
- // call the fn
- fn();
- }
- }(0));
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement