frontAnswerer/src/widgets/pollForSelector.ts
nflnkr d1c70c264d add widget selector polling
split button widget class into fixed and normal
2024-04-28 16:25:59 +03:00

26 lines
731 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const SELECTOR_POLLING_INTERVAL = 5000;
export function pollForSelector(
selector: string,
selectorPollingTimeLimit: number,
onSuccess: (element: Element) => void,
) {
const deadline = Date.now() + selectorPollingTimeLimit * 1000;
const interval = setInterval(() => {
const element = document.querySelector(selector);
if (Date.now() > deadline) {
clearInterval(interval);
console.error(`Не удалось найти элемент ${selector} для вставки виджета`);
return;
}
if (!element) {
return;
}
clearInterval(interval);
onSuccess(element);
}, SELECTOR_POLLING_INTERVAL);
}