场景是:每小时后用户将获得一个弹出窗口(javascript / jQuery)。如果用户没有响应弹出窗口,则会话被破坏。现在我遇到一个问题,如果没有回答弹出窗口,如何触发功能。请帮忙。到目前为止,我的代码如下所示。
function popitup() {
newwindow=window.open('/Untitled-3.html','name','height=500,width=500');
if (window.focus) {newwindow.focus()}
return false;
}
//popitup();
function showpop(){
var x;
var r=confirm("Press a button!");
if (r==true)
{
x="You pressed OK!";
}
else
{
x="You pressed Cancel!";
closeWin();
}
setTimeout(closeWin,15000);
}
function closeWin()
{
newwindow.close();
}
setInterval ( showpop, 10000 );
答案 0 :(得分:0)
在popitup
函数中使用 setTimeout ,如下所示
function popitup() {
setTimeout(closeWin,60000);// here I've taken 60 seconds as the response time to call closeWin function
newwindow = window.open('/abc.html', 'name', 'height=500,width=500');
if (window.focus) {
newwindow.focus()
}
return false;
}
打开弹出窗口60秒后,将调用closeWin函数,你可以调用这样的任何函数。
<强>更新强>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<title>Untitled Document</title>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
var myVar;
function popitup() {
newwindow = window.open('Default.aspx', 'name', 'height=500,width=500');
if (window.focus) { newwindow.focus() }
myVar = setInterval(showpop, 10000);
return false;
}
//popitup();
function showpop() {
var x;
//debugger;
var counter = setTimeout(closeWin, 5000);
$(function () {
$("#dialog-confirm").dialog({
resizable: false,
height: 140,
modal: true,
buttons: {
Ok: function () {
$(this).dialog("close");
clearTimeout(counter);
},
Cancel: function () {
$(this).dialog("close");
closeWin();
clearInterval(myVar);
}
}
});
});
//setTimeout(function(){closeWin()},15000);
}
function closeWin() {
//debugger;
newwindow.close();
}
</script>
</head>
<body>
<div id="dialog-confirm" title="do you wan to continue?">
</div>
<a href="#" onclick="popitup()">blah</a>
</body>
</html>
答案 1 :(得分:0)
而是setInterval
尝试setTimeOut(closeWin, 10000);
这将在10秒后调用closeWin函数。
另类,你可以尝试这样的事情:
res = confirm("You Message here"); if(res==true) {window.location="abc.html"}
setTimeout(function(){window.location="abc.html"},10000);