add repeat for gosuslugi

This commit is contained in:
tmshv committed 2023-08-16 20:09:08 +03:00
1 parent ac3bbb825d
commit 320768b6db
1 file changed
+27 -3
+27 -3
View File
@@ -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<void> {
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<GosuslugiProps> = () => {
useEffect(() => {
const id = setTimeout(() => {
let mount = true
repeat(1000, 50, () => {
if (!mount) {
return
}
// This call taken from getCode <script>Widget("https://pos.gosuslugi.ru/form", 312519)</script>
(window as any).Widget("https://pos.gosuslugi.ru/form", 312519)
}, 100)
})
return () => {
clearTimeout(id)
mount = false
}
}, [])