简体   繁体   中英

How to display a popup message after a delay

I want to display a popup message when the user visits my site and next popup after 3 hours.
I am half way done, as I have implemented the popup message displaying for the first visit.
Please guide me how can I display a popup after delaying it for 3 hours?

Thanks.

If you are using javascript you can use local storage by doing:

$(document).ready(function() {
    var visited = localStorage['visited'];
    if (!visited) {
        localStorage['visited'] = Math.round(+new Date()/1000)
        //Run initial pop up code
    }


   window.setInterval(function(){
        visited = localStorage['visited'];
        if(visited != "finished") {
            if (visited + (3600*3) < Math.round(+new Date()/1000)) {
                //Run popup code for after 3 hours
                localStorage['visited'] = "finished";
            }
        }
   }, 60000);


});

as others have already suggested, setInterval is probably what you are looking for. see this jsFiddle for an example: http://jsfiddle.net/dswknovp/ good luck!

HTML:

<span id="alertText"></span>

JS:

var myTimer = 1000*3; // every 3 seconds for testing, use: 1000*60*60*3; //for 3 hours

var myStart = setInterval(function(){
    myAlarm()
}, myTimer);

function myAlarm() {
    document.getElementById("alertText").innerHTML += "-Time elapsed-";
    //you can change this to, for example: alert("Time is up");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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