2013-05-29 44 views
4

我正在製作一個網頁,在該網頁中我必須打開一個彈出窗口,並且應該保持打開時間爲8秒(8000毫秒)。如何在特定時間段內自動打開和關閉彈出窗口

經過這段時間之後,彈出窗口應該關閉。然後再次4秒後,我需要打開相同的彈出窗口另外8秒。

我想提出一個彈出窗口前有延遲(4秒)自動打開和關閉,並彈出窗口必須在8秒

這裏可以保持打開是我的代碼:

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

我我熟悉像setInterval()setTimeout()這樣的java腳本函數,但在這種情況下我沒有發現它們有用。 我也允許我的瀏覽器打開彈出窗口,但是這個腳本會打開一個彈出窗口並隨着時間的推移關閉它。 請幫我找出我的代碼中的故障。

謝謝。

+0

的可能的複製特定的時間間隔後關閉彈出窗口【如何自動關閉網頁(http://stackoverflow.com/questions/ 14621554 /如何自動關閉網頁) – davidcondrey

回答

4

你的代碼是在尋找格式化不錯,但嘗試把一些努力在你的代碼。
嘗試使用它,如下所示,這裏是你的整個代碼,我已經在我的系統上試過了,它是工作

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

嘗試這些:

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 
} 

您當前call()每次它執行時會創建一個新的時間間隔。

0

你可以寫爲「情景」:如果你在某一步驟需要重複循環

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); 
} 

,你當然可以使用的下一個(0)。

當然還有必須的功能showPopup和hidePopup

+0

該解決方案極其冗餘。我不確定這是個好主意! Teemu答案似乎更好,避免進行大量不需要的測試。你可以閱讀這個如果你想要的:http://james.padolsey。com/javascript/how-to-avoid-switch-case-syndrome/ 無論如何,您確切知道將要調用的交換機的下一個「案例」,所以我不認爲switch語句是好的對於這種情況。 – mithrop

+0

我同意,這是多餘的 - 但它是普遍的。當然,它可以寫得更好,其中f.e.場景步驟被聲明爲數組或對象... –

1

你是不是你指定的時間間隔任何變量,而無需這些區間,他們將繼續與沒有辦法清除它們的運行下去一個參考和。使用setTimeout做一個你想做的事情的方法可能會更好,因爲打開/關閉和重新打開之間的時間段都是不同的,你也可以不間斷地運行間隔。下面的代碼創建一個無限期運行的循環,直到你希望它停止。

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(); 

停止循環,隨時可以調用

popup.clearLoop(); 
1

嗨,你可以使用這個下面的代碼

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