20 lines
489 B
JavaScript
20 lines
489 B
JavaScript
|
import { frame, cancelFrame } from '../frameloop/frame.mjs';
|
||
|
|
||
|
/**
|
||
|
* Timeout defined in ms
|
||
|
*/
|
||
|
function delay(callback, timeout) {
|
||
|
const start = performance.now();
|
||
|
const checkElapsed = ({ timestamp }) => {
|
||
|
const elapsed = timestamp - start;
|
||
|
if (elapsed >= timeout) {
|
||
|
cancelFrame(checkElapsed);
|
||
|
callback(elapsed - timeout);
|
||
|
}
|
||
|
};
|
||
|
frame.read(checkElapsed, true);
|
||
|
return () => cancelFrame(checkElapsed);
|
||
|
}
|
||
|
|
||
|
export { delay };
|