如何为延迟加载的Google Form iframe添加倒计时提示?
解决方案
直接替换成下面的代码,就能实现动态倒计时提示,倒计时结束后自动加载Google Form:
<div id="countdown" style="font-size:18px; padding:20px; text-align:center;">请等待...剩余10秒!</div> <iframe id="iframe" src="" height="2350px" width="100%" style="display:none;"></iframe> <script> // 初始化倒计时秒数 let remainingSeconds = 10; const countdownEl = document.getElementById('countdown'); const iframeEl = document.getElementById('iframe'); // 倒计时更新函数 const countdownTimer = setInterval(() => { remainingSeconds--; if (remainingSeconds > 0) { countdownEl.textContent = `请等待...剩余${remainingSeconds}秒!`; } else { // 倒计时结束,加载表单并清理定时器 clearInterval(countdownTimer); iframeEl.src = "google form link"; // 替换成你的Google Form链接 countdownEl.style.display = 'none'; iframeEl.style.display = 'block'; } }, 1000); </script>
关键改动说明:
- 新增
countdowndiv元素,专门用来展示动态倒计时提示,初始显示“请等待...剩余10秒!” - 给iframe添加初始隐藏样式,避免空白占位,倒计时结束后再显示表单
- 用
setInterval替代单一的setTimeout,实现每秒自动更新剩余秒数 - 倒计时归0时,自动触发iframe加载、隐藏倒计时提示,并显示表单
内容的提问来源于stack exchange,提问作者Hamza VFX
相关产品推荐
相关产品推荐

