How to open and close pop-ups automatically with a certain period of time

I am creating a webpage in which I need to open a popup and it should remain open for 8 seconds (8000 ms).

After this period of time, the popup should be closed. Then again after 4 seconds I need to open the same popup for another 8 seconds.

I want to put some delay (4 seconds) so that the popup window opens and closes automatically, and the popup window should remain open for 8 seconds.

Here is my code:

<html> <head> <script> function call() { popup = window.open('http://www.google.co.in'); setInterval(function() {wait();},4000); } function caller() { setInterval(function() {call();},5000); } function wait() { popup.close(); } </script> </head> <body onload="caller();"> </body> </html> 

I am familiar with java script functions like setInterval() and setTimeout() , but in this case I did not find anything useful. I also allowed the browser to open popups, but this script opens a popup and closes it over time. Please help me find out the failure in my code.

Thanks.

+4
source share
5 answers

Your code is good at formatting, but try to put some effort into your code.
Try using it as follows. Here is your whole code, I tried it on my system and it works .

 <html> <head> <script> function call() { popup = window.open('http://www.google.co.in'); setTimeout(wait, 8000); } function caller() { setInterval(call, 12000); } function wait() { popup.close(); } </script> </head> <body onload="caller();"> </body> </html> 
+4
source

Try the following:

 function call() { popup = window.open('http://www.google.co.in'); setTimeout(wait, 8000); // closes the pop-up after 8 secs delay } function caller(){ setInterval(call, 12000); // opens a pop-up on every 12th second } 

Your current call() creates a new interval every time it is executed.

+2
source

You do not assign your intervals to any variables, without reference to these intervals, they will continue to work for an unlimited time without the possibility of clearing them. The method of doing what you want is likely to be better with setTimeout, since the periods between opening / closing and re-opening are all different, and you also have no problem with constant intervals. The code below creates a loop that will run indefinitely until you want to stop it.

 var popup = { open : function() { // Open popup this.popupWindow = window.open('http://www.google.co.in'); // Return a timeout function return setTimeout(function() { // run close function on completion of timeout popup.close(); }, 8000); }, close : function() { this.popupWindow.close(); // Assign timeout to local variable this.timeout = this.loop(); }, loop : function() { var self = this; // On first run open popup immediately, or wait 4 seconds and restart // the loop if (self.active === true) { return setTimeout(function() { self.timeout = self.open(); }, 4000); } else { self.active = true; self.timeout = self.open(); } }, clearLoop : function() { clearTimeout(this.timeout); this.active = false; } }; popup.loop(); 

to stop the cycle anytime you can call

 popup.clearLoop(); 
+1
source

Hi, you can close the popup at a certain interval using this code below

  <button class="btn btn-success" type="submit" id="spk_button" onclick="javascript:setTimeout(function(){window.close()},3000);">Save</button> 
+1
source

You can write it as a "script":

 function next(state) { switch(state) { case 0: showPopup(); setTimeout("next(1);",8000); break; case 1: hidePopup(); setTimeout("next(2);",4000); break; case 2: showPopup(); setTimeout("next(3);",8000); break; ...etc... } } /* this you have to call at start */ function init() { next(0); } 

If you need to repeat the cycle in several steps, you can, of course, use the following (0).

And of course, there should be showPopup and hidePopup functions

0
source

Source: https://habr.com/ru/post/1483294/


All Articles