简体   繁体   中英

Javascript timed popup alert with location.href

I'm making a message that pops up after 10 seconds and after "ok" is clicked the user is redirected to a different page. I have this so far.

if (setTimeout(function() { !alert("my message"); }, 10000)); {
location.href = "/some/url"
}

It just redirects right when the page loads, no message and no "ok" click. Any ideas how to get this working?

I guess it should be something like this.

setTimeout(function() { 
    if (window.confirm("Go?")) { 
      location.href = "/some/url";
    }
}, 10000);

the setTimeout returns the id of the timeout. So you are just checking if the id is truthy. That code does not have a wait until the user clicks the button.

If you want to wait until after the user hits the okay on the alert, than you need to put the location.href line after the alert.

window.setTimeout( function () {
   alert("x");
   window.location.href = "/a/b/c";
}, 10000);

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