Advertisement
Guest User

Untitled

a guest
Aug 14th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var limitLoop = function (fn, fps) {
  2.  
  3.     // Use var then = Date.now(); if you
  4.     // don't care about targetting < IE9
  5.     var then = new Date().getTime();
  6.  
  7.     // custom fps, otherwise fallback to 60
  8.     fps = fps || 60;
  9.     var interval = 1000 / fps;
  10.  
  11.     return (function loop(time){
  12.         requestAnimationFrame(loop);
  13.  
  14.         // again, Date.now() if it's available
  15.         var now = new Date().getTime();
  16.         var delta = now - then;
  17.  
  18.         if (delta > interval) {
  19.             // Update time
  20.             // now - (delta % interval) is an improvement over just
  21.             // using then = now, which can end up lowering overall fps
  22.             then = now - (delta % interval);
  23.  
  24.             // call the fn
  25.             fn();
  26.         }
  27.     }(0));
  28. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement