> Show a deadline timer's countdown, charge its current tier's price at checkout, and react when the tier changes, with attributes or plandalf.promo().

Source: https://plandalf.com/docs/developers/promos-from-code

Developers

# Deadline timers from code

A deadline timer's price and deadline move through tiers over time. The SDK shows the countdown, charges the tier that's on now, and tells your page when the tier changes. Most sites only need two attributes; the code below is for when you want more control. Setting up the deadline timer itself is in [Deadline timers](https://plandalf.com/docs/promos/overview).

## With attributes

```html
<!-- the live countdown -->
<div data-plandalf-promo="YOUR-PROMO"></div>

<!-- a buy button that charges the current tier's price -->
<button data-plandalf-present="YOUR-CHECKOUT" data-plandalf-apply-promo="YOUR-PROMO">Buy now</button>
```

Attribute

Values

What it does

`data-plandalf-apply-promo`

Promo slug. On a `data-plandalf-present` or `data-plandalf-mount` element, the checkout it opens uses the promo's active tier price. The SDK also starts the promo's redirect monitor.

`data-plandalf-hide-on-expire`

Present on a promo countdown element: the SDK hides the element when the countdown ends (or the promo has no live config).

`data-plandalf-no-redirect`

Present on a promo element: the auto-scan skips it, so no countdown mounts and no redirect monitor starts for it.

`data-plandalf-promo`

Promo slug. On a plain element the SDK renders the promo's live countdown inside it and watches the active tier. On a `data-plandalf-present` / `data-plandalf-mount` element it is the deprecated spelling of `data-plandalf-apply-promo`.

`data-plandalf-style`

JSON object of style overrides, e.g. `{"bg":"#000"}`. On a promo widget it adjusts the saved promo style for that one widget; on a referral badge host it sets the badge's CSS variables (`--pf-<key>`). Malformed JSON is ignored.

## Charge the tier's price from code

Pass the deadline timer's slug as `promo`. The SDK looks up the tier that's on now for this visitor and uses its price.

```js
plandalf.present('YOUR-CHECKOUT', { promo: 'YOUR-PROMO' });
plandalf.mount('#checkout', 'YOUR-CHECKOUT', { promo: 'YOUR-PROMO' });
```

## Read and watch a deadline timer

`plandalf.promo(slug)` returns a handle. Await it for the deadline timer's current state, loop over `.events` to hear when the tier changes, and call `.close()` to stop.

```js
const promo = plandalf.promo('YOUR-PROMO');

const state = await promo; // null when the promo has no live tier
if (state?.activeTier) {
  priceLabel.textContent = state.activeTier.price;
  console.log(state.remaining, 'seconds left');
}

for await (const event of promo.events) {
  if (event.type === 'tier-change') priceLabel.textContent = event.tier?.price ?? '';
  if (event.type === 'expired') buyButton.hidden = true;
}
```

The state has `activeTier` (its `label`, `price` and `redirect_url`, or `null` once the deadline timer has ended), `remaining` seconds, `spotsRemaining` for a deadline timer limited by quantity, and the deadline timer's `config`. Every field is in [`ResolvedPromo`](https://plandalf.com/docs/developers/sdk-reference#types).

There's one handle per deadline timer per page: calling `plandalf.promo()` again with the same slug, or adding another element with that slug, shares it. Elements with `data-plandalf-promo` or `data-plandalf-apply-promo` also receive `plandalf:tier-change` and `plandalf:expired` as bubbling DOM events, and `plandalf.from(element)` returns the same handle.

## The tier's page

A tier can have its own page, its **Destination URL**. While a deadline timer handle is open, the SDK checks it every second and sends the visitor to the current tier's page when they're on a different one: when they arrive on an old link, or when the tier changes while they're reading. It doesn't do this while a Plandalf checkout is open.

To read a deadline timer without moving the visitor, pass `redirect: false`. Options go through the function form, since `plandalf.promo()` takes only the slug:

```js
const promo = plandalf('promo', 'YOUR-PROMO', { redirect: false });
```

On an element, `data-plandalf-no-redirect` makes the SDK skip it entirely. To keep some pages of your site from redirecting, list them in the deadline timer's **Ignore redirect on these pages** setting.

## Put the countdown where you want it

Pass `target` to render the countdown into an element, with optional `style` overrides on top of the deadline timer's saved style. It's the same countdown `data-plandalf-promo` shows.

```js
plandalf('promo', 'YOUR-PROMO', { target: '#timer', style: { bg: '#000' } });
```

In React, `useCountdown(slug)` gives you a `ref` to attach to an element, and `usePromo(slug)` gives you the state without redirecting. See [React](https://plandalf.com/docs/developers/react).

Option

What it does

`target`

Element or selector to render the countdown into. Leave it out to watch it with no visible countdown.

`style`

Style overrides for this countdown, the same shape as `data-plandalf-style`.

`redirect`

`false` stops the redirect to the tier's page. Default `true`.

`interval`

How often to check it, in milliseconds. Default `1000`.

## One timer per visitor

A deadline timer can run its clock per visitor. The SDK keys it on the visitor's ref when it has one, and otherwise on the browser's anonymous id. Put `?pf_ref=` in your email links so a subscriber gets the same timer on every device:

```text
https://example.com/sale?pf_ref={{subscriber.email}}
```

More on refs, including `setRef`, in [Referrals and visitor refs](https://plandalf.com/docs/developers/referrals-from-code#visitor-ref).

Something wrong or missing? Email [hello@plandalf.com](mailto:hello@plandalf.com).
