2014-07-08 138 views
0

嗨,大家好我正在使用這個函數打開一個彈出窗口並在3秒後關閉它。彈出窗口成功打開,但settimeout函數不起作用,並最終不關閉打開的彈出窗口。 我在這裏做錯了什麼?Javascript settimeout不關閉彈出框

<script type="text/javascript"> 
    function popUp(id,entry,escape) 
    { 
popupWindow = window.open('process_concur.php?id='+id+'&entry='+entry+'&escape='+escape,'Concur','resizable=yes,scrollbars=yes,width=250,height=250'); 
     popupWindow.focus(); 
     setTimeout(function() { popupWindow.close();}, 3000); 
    } 

//reload the current window when the popup is closed. 
    function popUpClosed() { 
     window.location.reload(); 
     } 
    </script> 

這將打開彈出頁面(代碼)

<?php 
include 'classes/class.user.php'; 
$userMain = new user(); 
//get parameters 
$id = isset($_GET['id']) ? $_GET['id'] : ''; 
$entry = isset($_GET['entry']) ? $_GET['entry'] : ''; 
$escape = isset($_GET['escape']) ? $_GET['escape'] : ''; 
//now, $escape is sha1 and $entry is base64 encoded..decode entry and check if sha1 of decoded entry matches escape 
$dentry = base64_decode($entry); 
if(sha1($dentry)==$escape) 
{ 
    //process concur 
    if($userMain->reviewExists($id)) 
    { 
     if($userMain->increaseConcur($id)) 
     { 
      echo "Done"; 
      exit(); 
     } 
    } 
    else 
    { 
     //review doesnt no exist 
     echo "Some problems occured at id doesn't exist"; 
    } 
} 
else 
{ 
    echo "Some problems occured dentry"; 
} 
?> 

<script> 
window.onunload = function() { 
    if (window.opener && !window.opener.closed) { 
     window.opener.popUpClosed(); 
    } 
}; 
</script> 
+0

如果您使用jQuery模式,會不會更好? –

+6

@MihaiIorga這與問題有什麼關係? – feeela

+1

調用'popUpClosed()'在哪裏? – edi9999

回答

0

您的代碼看起來是正確的。確保沒有別的東西干擾這個過程。

此外,應在窗口發送關閉信號後調用popUpClosed()函數。

<script type="text/javascript"> 

function popUp(id,entry,escape) 
{ 
var popupWindow = window.open('process_concur.php?id='+id+'&entry='+entry+'&escape='+escape,'Concur','resizable=yes,scrollbars=yes,width=250,height=250'); 
popupWindow.focus(); 
setTimeout(function() { popupWindow.close(); popUpClosed();}, 3000); 
} 

//reload the current window when the popup is closed. 
function popUpClosed() { 
    window.location.reload(); 
} 

popUp(); 
</script> 
+3

您的第一段不是答案。如果你不能重現問題,那麼投票結束它,不要發佈答案,說它應該工作。其餘部分是關於最佳實踐的評論,而不是對問題的回答。 – Quentin

+0

您的解決方案使工作變得更糟。現在窗戶不斷打開,甚至沒有打電話就關閉了。 – user3605847

+0

@ user3605847 - 請理解您正在粘貼的代碼。最後一行'popUp()'會導致彈出窗口出現。刪除它並從你自己的代碼中調用該函數。 – Kami