rifraf
A simple requestAnimationFrame (rAF) wrapper/polyfill with added iteratee creator.
Polyfill notes
- The polyfill is a very naïve setTimeout wrapper. For a more robust polyfill, I recommend the raf module.
- The default "frame-rate" for the polyfill is 120Hz or an 8ms delay.
API
All examples below assume that you've already required rifraf: var rifraf = require('rifraf');
The Basics
<Function> fn
, <Object:optional> ctx
)
request(Queues a callback to run before the next frame. Returns the rAF (or timeout, if polyfilled) handle. Pre-binds optional context object, if provided.
// rifraf.request returns the runtime-assigned handle that can be used to cancel the callbackvar handle = rifraf;
handle
)
cancel(Cancels a previously request using the returned handle.
// where handle is the return value of a rifraf.request callrifraf;
The Extras
<Function> fn
, <Object:optional> ctx
)
iteratee(alias: deferred
Used to defer expensive iterations or event handlers that need to wait until after all current DOM operations complete. Returns a new function that when called queues fn bound with ctx or its own this and its arguments.
// with context object; // without context object;
<Function> fn
, <Object:optional> ctx
, <Number:optional> _delay
)
delay(When you want to defer a function call, but your desired frame rate differs from native, delay
is for you. Pre-binds context, if provided.
// with contextrifraf; // the next two are equivalent and will set the delay to ~24msrifraf; rifraf;
<Function> fn
, <Object:optional> ctx
, <Number:optional> delay
)
delayed(Used like iteratee
, but when you want to delay
not simply defer to next native frame. Call signature matches delay
.
var delayedDefault = rifraf; ; var delayed24ms = rifraf;
sync120Hz()
Sets default delay time for delay
, delayed
(and polyfilled request
and iteratee
) methods to 8ms (roughly: 1000 / 120).
sync60Hz()
Sets default delay time for delay
, delayed
(and polyfilled request
and iteratee
) methods to 16ms (roughly: 1000 / 60).
sync30Hz()
Sets default delay time for delay
, delayed
(and polyfilled request
and iteratee
) methods to 33ms (roughly: 1000 / 30).
<Number> delay
)
sync(Sets default delay time for delay
, delayed
(and polyfilled request
and iteratee
) methods to {delay}
ms.