2014-04-18 35 views
0

我有一个通知框,当用户单击链接时弹出。我决定改变它,以便弹出窗口在一段时间后自动弹出。Javascript - 在运行脚本前只等待一段时间

<div id="container"> 
</p> 
<ul> 
    <li> 
     <a href="#" id="add-sticky">Add sticky notification</a>: Doesn't run on a fade timer. Just sits there until the user manually removes it by clicking on the (X).  
    </li> 
    <li> 
     <a href="#" id="remove-all">Remove all notifications</a>   
    </li> 
</ul> 

这是原来的链接,我不得不在现场。我有另一个Javascript文件,用于侦听添加粘性ID。 (这工作非常好)。这是一个运行在自动弹出

setInterval("popup()", 2000); 
function popup() { 
      var el = document.getElementById('add-sticky'); 



// Firefox 
if (document.createEvent) { 
var event = document.createEvent("MouseEvents"); 
event.initEvent("click", true, true); 
el.dispatchEvent(event); 
} 
// IE 
else if (el.click) { 
el.click(); 

} 

      } 

然而,这使得在弹出的运行激活一遍一遍只是文件的页面中的JavaScript文件。我怎样才能弹出运行约30秒后页面上登陆,然后等待大约一分钟,然后再运行另一个弹出?

除了我得到我似乎无法得到它做到这一点。任何帮助,你是伟大的

鲍比

+0

使用'setTimout',而不是'setInterval'。 –

回答

1

我会使用的setTimeout来代替,如果你只是想执行弹出两次

setTimeout(popup, 30000); // 30 seconds 
function popup() { 
    // Show your popup 

    // if (needToShowPopUpAgain) 
      setTimeout(popup, 60000); // 1 minute 



} 
+0

非常感谢你,如果我想设置它,所以它会在我需要更改后每隔1分钟不断弹出。 – user3487006

+0

如果你想连续弹出,我会使用setInterval。每当调用clearInterval时,都可以取消setInterval。 – user715489

相关问题