Guest User

Untitled

a guest
Jan 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. /* Use this to cause a function to fire no more than once every 'ms' milliseconds.
  2. For example, an expensive mousemove handler:
  3.  
  4. $('body').mouseover(ratelimit(function(ev) {
  5. // ...
  6. }, 250));
  7. */
  8.  
  9. function ratelimit(fn, ms) {
  10. var last = (new Date()).getTime();
  11. return (function() {
  12. var now = (new Date()).getTime();
  13. if (now - last > ms) {
  14. last = now;
  15. fn.apply(null, arguments);
  16. }
  17. });
  18. }
Add Comment
Please, Sign In to add comment