Skip to content

waitFor() & watchFor()

Both functions wait for DOM elements before running a callback. waitFor polls; watchFor uses a MutationObserver.

waitFor()

Polls until all given CSS selectors match at least one element, then fires a callback.

Signature

ts
waitFor(selectors: string[], callback: () => void): void

Parameters

ParameterTypeDescription
selectorsstring[]Array of CSS selector strings. All must match before the callback fires.
callbackFunctionInvoked once every selector matches.

Usage

js
import { waitFor } from '@sogody/experiment-framework/framework';

waitFor(['.target-selector', '.price-wrapper'], () => {
    // All elements exist - safe to proceed
    const target = document.querySelector('.target-selector');
});

Returns

void

Since

v2.0.0

  • watchFor() - event-driven alternative with timeout support
  • runScript() - wraps the experiment entry point and ensures DOM readiness

Polling interval

Checks every 50 ms. There is no timeout - if a selector never matches, the callback never fires. Use watchFor if you need a timeout.


watchFor()

Waits for a single CSS selector with a MutationObserver instead of polling.

Signature

ts
watchFor(
    selector: string,
    callback: (element: Element) => void,
    options?: { subtree?: boolean; timeout?: number }
): void

Parameters

ParameterTypeDefaultDescription
selectorstring-CSS selector to watch for.
callbackFunction-Called with the matched element once it appears.
options.subtreebooleanfalseObserve all descendants of document.body, not only direct children. Use this only when necessary.
options.timeoutnumber6000Auto-disconnects the observer after this many milliseconds to prevent memory leaks.

Usage

js
import { watchFor } from '@sogody/experiment-framework/framework';

watchFor('.late-loading-element', (element) => {
    // element is the matched DOM node
    element.insertAdjacentHTML('afterend', '<div>Experiment</div>');
});

With options:

js
watchFor('.deep-nested-element', (element) => {
    // ...
}, { subtree: true, timeout: 8000 });

Returns

void

Since

v2.0.0

  • waitFor() - polling-based alternative that supports multiple selectors
  • runScript() - wraps the experiment entry point and ensures DOM readiness

Observer cleanup

The observer disconnects after timeout milliseconds if the element never appears.


waitFor vs watchFor

waitForwatchFor
MechanismRecursive setTimeout (50 ms)MutationObserver
Multiple selectorsYesNo (single selector)
Callback receives elementNoYes
Timeout supportNoYes (default 6000ms)
Best forSimple cases, stable pagesSPA navigations, deep DOM trees

Choosing a helper

Use waitFor when several elements should appear soon after page load. Use watchFor for one element that appears after an interaction, route change, or lazy render.

Internal tool - Samsung / Sogody experimentation team

Help improve the frameworkShare an idea or friction you encountered.Share framework feedback(opens in a new tab)