lefengyang
2026-08-20 d01a28cb030b2cb0ff1d42273c877a0f728519a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { wait } from "gs-base";
 
export interface IForWaitFindOption {
  minLen?: number;
  delay?: number;
  count?: number;
  by?: Element | Document;
}
 
export async function forWaitFind<T extends HTMLElement = HTMLElement>(selector: string, option?: IForWaitFindOption): Promise<T[]> {
  const { minLen = 1, delay = 50, count = 50, by = document } = option || {};
  for (let i = 0; i < count; i++) {
    const elements = by.querySelectorAll<T>(selector);
    if (elements.length >= minLen) {
      return Array.from(elements);
    }
    await wait(delay);
  }
}