From 320768b6dbc2e337178bd65bf3eaf7e2ac5a0a7f Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Wed, 16 Aug 2023 20:09:08 +0300 Subject: [PATCH] add repeat for gosuslugi --- src/components/Gosuslugi/index.tsx | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/components/Gosuslugi/index.tsx b/src/components/Gosuslugi/index.tsx index 5c98ab4a..30d89733 100644 --- a/src/components/Gosuslugi/index.tsx +++ b/src/components/Gosuslugi/index.tsx @@ -2,17 +2,41 @@ import { useEffect } from "react" import { getCode } from "./lib" import InnerHTML from "dangerously-set-html-content" +async function sleep(ms: number) { + return new Promise(resolve => { + setTimeout(resolve, ms) + }) +} + +async function repeat(delay: number, times: number, fn: () => void): Promise { + try { + fn() + } catch (error) { + if (times === 0) { + throw error + } + + await sleep(delay) + return repeat(delay, times - 1, fn) + } +} + export type GosuslugiProps = {} export const Gosuslugi: React.FC = () => { useEffect(() => { - const id = setTimeout(() => { + let mount = true + repeat(1000, 50, () => { + if (!mount) { + return + } + // This call taken from getCode (window as any).Widget("https://pos.gosuslugi.ru/form", 312519) - }, 100) + }) return () => { - clearTimeout(id) + mount = false } }, [])