You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现点击按钮触发广告并配置3秒冷却后二次点击跳转不同地址

实现方案

以下是直接可用的整合代码,已经调整了原有广告脚本的加载逻辑,避免点击触发时document.write覆盖整个页面的问题:

<!-- 页面按钮示例,可替换为你自己的按钮样式 -->
<button id="customActionBtn">点击访问</button>

<script>
// 可修改配置项
const SECOND_JUMP_URL = '替换为第二次点击要跳转的目标地址';
const COOLDOWN_MS = 3000; // 冷却时长3秒,可自行调整数值

// 状态控制变量
let isCooling = false;
let isAdTriggered = false;

// 绑定按钮点击事件
document.querySelector('#customActionBtn').addEventListener('click', function() {
    // 冷却期内直接拦截点击
    if (isCooling) return;

    if (!isAdTriggered) {
        // 第一次点击触发广告逻辑
        loadAd();
        // 开启冷却
        isCooling = true;
        setTimeout(() => {
            isCooling = false;
            isAdTriggered = true;
        }, COOLDOWN_MS);
    } else {
        // 冷却结束后点击跳转指定地址
        window.location.href = SECOND_JUMP_URL;
    }
});

// 广告加载逻辑
function loadAd() {
    window.atOptions = {
        'key' : '替换为你自己的广告key',
        'format' : 'iframe',
        'height' : 300,
        'width' : 160,
        'params' : {}
    };
    const adScript = document.createElement('script');
    adScript.type = 'text/javascript';
    adScript.src = (location.protocol === 'https:' ? 'https' : 'http') + '://www.variousformatscontent.com/' + atOptions.key + '/invoke.js';
    document.body.appendChild(adScript);
}
</script>
  • 你需要自行替换代码中的替换为第二次点击要跳转的目标地址为实际跳转地址
  • 替换替换为你自己的广告key为你的真实广告授权key
  • 如需调整冷却时长,修改COOLDOWN_MS的数值即可,数值单位为毫秒

内容的提问来源于stack exchange,提问作者BeamCRASH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 00:06:08