【问题标题】:Show a popup on page load and then show and hide it at regular time intervals在页面加载时显示一个弹出窗口,然后定期显示和隐藏它
【发布时间】:2016-11-01 21:17:05
【问题描述】:

我需要在页面首次加载时显示免责声明 div,然后在此之后定期显示和隐藏它。每三分钟显示一次将是一个很好的时间间隔。

我目前有这个代码

<script>
function popup(){

setTimeout(function(){
document.getElementById("disclaimer").style.display = "block";
},0);
}
function hidePopup(){
document.getElementById("disclaimer").style.display = "none";
}
</script>

然后我需要在大约 3 分钟后继续显示弹出窗口。

【问题讨论】:

标签: javascript jquery


【解决方案1】:

试试这个

//start showing popup
popup();

function popup() {

  document.getElementById("disclaimer").style.display = "block";

  console.log("wait 3 seconds then hide");

  setTimeout(hidePopup, 3000);

}

function hidePopup() {

  document.getElementById("disclaimer").style.display = "none";

  console.log("wait 3 minutes then show popup again");

  setTimeout(popup, 3 * 60 * 1000);

}
&lt;div id='disclaimer'&gt;Disclaimer Div&lt;/div&gt;

【讨论】:

  • 这是提供的最佳答案,应该被接受
【解决方案2】:

您可以使用 setInterval 以指定的毫秒间隔异步运行代码。然后使用 setTimeout 延迟函数,让 pop 显示一段时间。

//function runs every 3 minutes
setInterval(function(){ 

   //function waits 1 second
   setTimeout(function(){
      popup();
   }, 1000);

   hidePopup();
}, 180000);

此函数将每 180000 毫秒(3 分钟)运行一次,并显示 1000 毫秒(1 秒)的弹出窗口。

【讨论】:

    【解决方案3】:

    这里我将如何仅使用一个功能来完成

    sn-p 1(如果需要,将时间间隔更改为多少秒)

    var dis_div = document.getElementById("disclaimer");
    function init_disclaimer() {
      if (dis_div.style.display == "block"){
        dis_div.style.display = "none"
        }
      else {
        dis_div.style.display = "block"
      }
    }
    inter=setInterval(init_disclaimer,500)
    <div id="disclaimer">Disclaimer
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 2013-05-13
      • 2012-09-30
      • 2021-09-02
      • 2021-05-20
      相关资源
      最近更新 更多