简体   繁体   中英

How do I get my popup form to close permanently?

I'm making a pop-up 'subscribe to the mailing list' form for my website. I have a button that is supposed to close the form with this function:

function closeForm() {
  document.getElementById("mailsub").style.display = "none";
}

but, seemingly due to also having this function to open the form:

setInterval(function openForm() {
  document.getElementById("mailsub").style.display = "block";
}, 700);

when you close the form, it just opens again after 0.7 seconds.

Does anyone know how to make it so the form closes indefinitely while preserving the function to open the form?

Thanks in advance.

setInterval - This will run every 700 milliseconds . so use setTimeout. It will run once after 700milliseconds

setTimeout(function openForm() {
  document.getElementById("mailsub").style.display = "block";
}, 700);

setInterval will cause the functinon that open the popup to be called every 0.7 seconds. Change it to setTimeout, and it will only fire once:

setTimeout(function openForm() {
  document.getElementById("mailsub").style.display = "block";
}, 700);

You have to use toggle to set display none or block. And with that you can use it if true use block else none. I can quickly write code for you as well.

shouldOpen ? 'block' : 'none'

shouldOpen hold value true or false and that will be set on the basis of your business rule.

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