settimeout vs requestanimationframe

Solutions on MaxInterview for settimeout vs requestanimationframe by the best coders in the world

showing results for - "settimeout vs requestanimationframe"
Niko
15 Feb 2017
1// setTimeout:
2// 	Calls a function or evaluates an expression after a specified number of milliseconds.
3// 	Doesn't mean that the function will be called after the exactly specified interval.
4//	Can cause a browser to miss the frame.
5// requestAnimationFrame
6//	Works similarly to setTimeout 
7//	called right before the next repaint in the browser occurs.
8
9const timestamp = setTimeout(() => {...}); // runs as frequently as possible
10const requestId = requestAnimationFrame(() => {...}); // runs once per frame
11
12clearTimeout(timestamp); // clear previously added timeout
13cancelAnimationFrame(requestId); // cancel an animation frame request