Skip to content

Commit

Permalink
set interval to avoid block
Browse files Browse the repository at this point in the history
  • Loading branch information
iFwu committed Oct 7, 2024
1 parent c817ce6 commit 43ecc70
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/hooks/useOpenCV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function useOpenCV() {
}
};
useEffect(() => {
let intervalId: number;
const initialize = async () => {
if (typeof cv !== 'undefined') {
await new Promise<void>((resolve) => {
Expand All @@ -31,13 +32,20 @@ export function useOpenCV() {
await onOpenCVReady();
} else {
document.addEventListener('opencv-loaded', onOpenCVReady);
intervalId = setInterval(() => {
if (!isLoading && typeof cv !== 'undefined') {
clearInterval(intervalId);
onOpenCVReady();
}
}, 500);
}
}

initialize();

return () => {
document.removeEventListener('opencv-loaded', onOpenCVReady);
clearInterval(intervalId);
};
}, []);

Expand Down

0 comments on commit 43ecc70

Please sign in to comment.