aherigon
aherigon

Reputation: 75

Run a javascript timer only once on aspx page

I have a function in my Site.Master page that displays a popup after 30 sec. But each time I load a subpage the timer statrs over and pops up again after the 30 sec.

I need it to display once, the whole time they are visiting the website. here is my code.

setTimeout(function() {
    // run event......
}, 30000);

Upvotes: 0

Views: 103

Answers (2)

suff trek
suff trek

Reputation: 39767

You can try setting a flag, for example in a local storage:

if (localStorage.getItem("popup") != "1") {

   localStorage.setItem("popup", "1");

   setTimeout(function() {
    // run event......
   }, 30000);
}

EDIT:

If you want to make sure that popup would show up at least once, use this version instead:

if (localStorage.getItem("popup") != "1") {
   setTimeout(function() {
      localStorage.setItem("popup", "1");
      // run event......
   }, 30000);
}

this way the flag will only be set if popup actually shows.

Upvotes: 2

Rob M.
Rob M.

Reputation: 36511

Probably would be easiest to use a session cookie to determine if the user has already seen the message:

if(!document.cookie.match(/sawMessage/)) {
   setTimeout(function() {
     // run event......
     document.cookie="sawMessage=true";
   }, 30000);
}

Upvotes: 2

Related Questions