Skip to content

Latest commit

 

History

History
63 lines (41 loc) · 1.07 KB

README.md

File metadata and controls

63 lines (41 loc) · 1.07 KB

raft

Or "Request Animation Frame Throttle".

Throttle a function to be executed at most once per animation frame.

Install

With component(1):

$ component install darsain/raft

Usage

var something = document.querySelector('.something');

window.addEventListener('mousemove', raft(updateSomething));

function updateSomething(event) {
	something.style.left = event.pageX;
	something.style.top = event.pageY;
}

API

raft(fn)

Returns a wrapper that will call fn at most once per animation frame.

Function will be executed once in next animation frame, and will receive last context (this) and arguments passed to the wrapper.

  • fn Function Function to wrap.

Returns Function

Example:

var a = { a: 'a' };
var b = { b: 'b' };
var c = { c: 'c' };
var log = raft(function (arg) {
	console.log(this, arg);
});

log.call(a, 'foo');
log.call(b, 'bar');
log.call(c, 'baz');

When next animation frame kicks in the console will log:

> Object { c: "c" } "baz"

License

MIT