Skip to content
/ raft Public

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

Notifications You must be signed in to change notification settings

darsain/raft

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

Stars

Watchers

Forks

Packages