> For the complete documentation index, see [llms.txt](https://helps.ptengine.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://helps.ptengine.com/cn/faq/experience-edit/anti-flicker.md).

# 闪烁现象及应对方法

做 A/B 测试或页面编辑时，访客可能**先看到原始内容、再突然变成改动后的内容** —— 闪一下。产品里把这个叫**闪动**，对应的功能叫**防止闪动**。这一页讲怎么把它压下去。

***

## 开始之前 <a href="#before-you-start" id="before-you-start"></a>

* **防止闪动默认是关的**，要在体验设置里手动打开
* 它的原理是**先用白屏盖住，等改动生效再显示** —— 不是“消除”闪动，而是用白屏换掉闪动
* **基础代码装在哪里，直接决定它能不能生效**（见下方速查表）

***

## 速查表：按你的安装方式选 <a href="#cheat-sheet" id="cheat-sheet"></a>

| 基础代码装在哪                        | 要做什么                            | 效果                                      |
| ------------------------------ | ------------------------------- | --------------------------------------- |
| **直接写在 `<head>` 里**（推荐）        | 打开**开启防止闪动**                    | ✅ 正常生效，**不需要**额外装防闪代码                   |
| **通过 GTM 安装**，或写在 `<body>` 里   | 在 `<head>` 里**另外加一段防闪代码**，再打开开关 | ✅ 生效                                    |
| 通过 GTM / `<body>` 安装，**只开了开关** | ——                              | ❌ **不生效**，而且会出现“原页面 → 白屏 → 新页面”这种更难看的效果 |

{% hint style="warning" %}
**同一页面上有多个体验时，只要其中一个开了防止闪动，整个页面就会生效。** 不用每个体验都开。
{% endhint %}

***

## 开关在哪 <a href="#where-is-the-toggle" id="where-is-the-toggle"></a>

{% stepper %}
{% step %}

### 进体验设置页，找到“创意”区块

点右侧的 **⚙ 设置**，菜单里有三项：**流量分配**、**测试设置**、**防止闪动设置**。

括号里直接标着当前状态，例如“防止闪动设置（关闭）”，不用点进去就能看出开没开。

<figure><img src="https://2044586328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnucbKsG9WGIoxCpzyoHL%2Fuploads%2Fgit-blob-e4e2db2d2ef2c0045d8bb593b7c28393658473e1%2Ffaq-anti-flicker-01.png?alt=media" alt="创意区块的设置菜单：流量分配 / 测试设置 / 防止闪动设置（关闭）"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### 点“防止闪动设置”，打开右下角的开关

弹窗里的蓝色提示框说的就是上面速查表那件事：**基础代码没装在 `<head>` 里时，直接开这个开关是没用的**。

<figure><img src="https://2044586328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnucbKsG9WGIoxCpzyoHL%2Fuploads%2Fgit-blob-e810064b1b2e8d1daa1c6c6ad8c481664d61fb2e%2Ffaq-anti-flicker-02.png?alt=media" alt="防止闪动设置弹窗：说明、前提提示与开启防止闪动开关"><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

***

## 什么时候该开 <a href="#when-to-enable" id="when-to-enable"></a>

| 情况                 | 开不开                    |
| ------------------ | ---------------------- |
| A/B 测试、重定向测试       | **开**                  |
| 用页面编辑改**首屏**内容     | **开**                  |
| 改的是页面下部、要滚动才看得到的内容 | 可以不开 —— 访客滚到那里时改动早就生效了 |

***

## 基础代码不在 `<head>` 里怎么办 <a href="#when-you-cannot-install-ptengine-tag-directly-in-head" id="when-you-cannot-install-ptengine-tag-directly-in-head"></a>

### 为什么会失效 <a href="#id-1.-phenomenon-and-cause" id="id-1.-phenomenon-and-cause"></a>

通过 GTM 安装、或把基础代码放在 `<body>` 里时，为了页面显示速度会**异步加载**，导致代码执行被推迟。等它跑起来，原始页面已经画出来了 —— 于是变成“原始页面 → 白屏 → 编辑后的页面”，比不开还难看。

### 解决方法 <a href="#id-2.-solution" id="id-2.-solution"></a>

**保持现有基础代码不动**，另外加一段防闪代码：

{% stepper %}
{% step %}

### 在 `<head>` 里直接放这段防闪代码

```html
<script src="https://js.ptengine.com/xxxxxxxx_antiFlicker.js"></script>
```

{% hint style="warning" %}
`xxxxxxxx` 是**档案 ID**，和你基础代码里的那串是同一个。
{% endhint %}
{% endstep %}

{% step %}

### 回 Ptengine 打开“开启防止闪动”

路径见上面[开关在哪](#where-is-the-toggle)。
{% endstep %}
{% endstepper %}

{% hint style="warning" %}
**防闪代码的超时时间是 4 秒。** 页面本身加载极慢的话，即使这样设置也还是会闪。

所以能直接把基础代码放进 `<head>` 就别绕道。
{% endhint %}

***

## 还是闪 <a href="#still-flickering" id="still-flickering"></a>

* **页面本身加载就很慢** —— 防闪代码超时 4 秒，超过就放弃等待直接显示。这种要先优化页面加载
* **改的是首屏元素、而且是用 JavaScript 改的** —— JS 改动在页面加载完成后才执行，天然会闪。首屏改动交给可视化编辑器，见 [代码模式的使用方法](/cn/experience/campaign/code-mode.md)
* **最根本的办法** —— 把基础代码从 GTM / `<body>` 挪到 `<head>` 里直接安装

***

## 相关页面 <a href="#related-pages" id="related-pages"></a>

* [基础代码的安装](/cn/start-guide/quick/tag-setup.md)
* [通过GTM安装](/cn/start-guide/othertag/tag-gtm.md)
* [推荐基础代码同步加载](/cn/faq/tracking-code/sync-load-recommendation.md) — 为什么异步会出问题
* [A/B 测试的高级设置](/cn/experience/campaign/create-new-2/advanced-settings.md)
* [代码模式的使用方法](/cn/experience/campaign/code-mode.md)
