2014-04-18 34 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

相關問題