简体   繁体   English

Javascript-等待设定的时间,然后仅运行一次脚本

[英]Javascript - Wait a set time before running script only once

I Have a notification box that pops up when a user click's on a link. 当用户单击链接时,我会弹出一个通知框。 I have decided to change this so that the pop up comes up automatically after a set time. 我决定更改此设置,以便在设置的时间后自动弹出该弹出窗口。

<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>

This is the original Link that i had on site. 这是我在网站上获得的原始链接。 I have another Javascript file that listens for the add-sticky ID. 我还有另一个Javascript文件,用于监听添加粘滞ID。 (This works perfectly fine). (这很好用)。 This is the javascript file that runs the automatic popup 这是运行自动弹出窗口的javascript文件

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();

}

           }

This however makes the pop run activate over and over again just files up the page. 但是,这会使弹出运行一次又一次地激活,仅将页面上载。 How can i get the pop up to run about 30secs after landing on page and then wait for about a min before running another pop up? 我如何才能使弹出窗口在登陆页面后运行约30秒,然后等待约一分钟,然后再运行另一个弹出窗口?

Apart from what i got i cannot seem to get it to do this. 除了我得到的东西,我似乎无法做到这一点。 Any help you be great 任何帮助你都很棒

Bobby 鲍比

I would use setTimeout instead if you just want to execute the pop up twice 如果您只想执行两次弹出窗口,我会改用setTimeout

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

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



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM