mediawiki-skins-Vector/resources/skins.vector.js/deferUntilFrame.js
Jon Robson c3e57e0ecd Merge skins.vector.es6 into skins.vector.js
With T178356 ES6 is the default, so these can now be
managed in the same module. Keeping them in the same module
will hopefully allow us to make more optimizations on the long
term.

Change-Id: I3fe9e50143b85b4cdc3d9171a60c3720a7c26b4b
2023-04-11 23:18:46 +00:00

23 lines
568 B
JavaScript

/**
* Helper method that calls a specified callback before the browser has
* performed a specified number of repaints.
*
* Uses `requestAnimationFrame` under the hood to determine the next repaint.
*
* @param {Function} callback
* @param {number} frameCount The number of frames to wait before calling the
* specified callback.
*/
function deferUntilFrame( callback, frameCount ) {
if ( frameCount === 0 ) {
callback();
return;
}
requestAnimationFrame( () => {
deferUntilFrame( callback, frameCount - 1 );
} );
}
module.exports = deferUntilFrame;