最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to open and close popups automatically with certain time period - Stack Overflow

programmeradmin0浏览0评论

I am making a web page, in which I have to open a popup window and it should remain open for a time period of 8 seconds(8000 ms).

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

I want to put some delay(4 seconds) before a popup open and closes automatically, and the popup window must be remain opened for 8 seconds

Here is my code:

<html>
<head>
    <script>
        function call()
        {
            popup = window.open('');         
            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 I've found none of them useful in this case. I've also allowed my browser to open Popups, but this script opens a popup window and closes it as fast as time passes. Please help me to find out the glitch in my code.

Thank you.

I am making a web page, in which I have to open a popup window and it should remain open for a time period of 8 seconds(8000 ms).

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

I want to put some delay(4 seconds) before a popup open and closes automatically, and the popup window must be remain opened 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 I've found none of them useful in this case. I've also allowed my browser to open Popups, but this script opens a popup window and closes it as fast as time passes. Please help me to find out the glitch in my code.

Thank you.

Share Improve this question edited May 29, 2013 at 7:42 Dreamwalker 3,0354 gold badges32 silver badges60 bronze badges asked May 29, 2013 at 7:20 PrateekPrateek 431 gold badge1 silver badge4 bronze badges 1
  • Possible duplicate of How to close automatically a webpage – davidcondrey Commented Mar 2, 2016 at 2:10
Add a comment  | 

5 Answers 5

Reset to default 4

Your code is looking good in formatting but try to put some efforts on your code.
Try to use it as given below, Here is your whole code, I've tried it on my system, and it is working.

<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>

Try these:

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's executed.

You are not assigning your intervals to any variables, without having a reference to these intervals they will continue running indefinitely with no way of clearing them. A method of doing what you want would probably be better with setTimeout as the periods between opening / closing and then re-opening are all different, also you have no problems with constantly running intervals. The below code creates a loop that will run indefinitely until you want it to stop.

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 loop at anytime you can call

popup.clearLoop();

Hi you can close the popup window after a particular interval using this below code

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

You can write it as a "scenario":

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 in some step to repeat cycle, you can of course use next(0).

And of course there have to be functions showPopup and hidePopup

发布评论

评论列表(0)

  1. 暂无评论