您好,我的以下 cmets 已删除。所以现在我已经找到了解决方案,我将把它提供给 cmets 下面的所有人:
我在纯 javascript 中创建了一个自定义确认框代码,它看起来和工作都很棒。这太容易了。但问题是,如果我必须在同一个网页上获得用户的 2 次确认,那么我必须编写单独的一组函数(每组 2 个函数名称不同)。
我看不出回调如何帮助我解决问题。回调函数也会立即执行,无需等待检查用户是否点击了确认或取消按钮。
我可以将回调函数名称传递给另一个使用 setTimeOut() 的函数并等待 15 分钟让用户单击确认或取消吗?单击按钮后,我可以将用户的选择记录在全局变量中,并使用 clearTimeOut() 立即执行下一部分。 15 分钟后,如果没有得到用户响应,则将其视为响应取消,这应该是安全的。有人想尝试解决这个问题吗?
我不接触或阅读 jQuery、Angular 等。请不要让我参与这些讨论。
好的,这是解决方案。主文件是 customConfirm.htm :-
<html>
<head>
<style>
/* ~~~~~~~~~~~~~~~ Style for the pop box with blackener ~~~~~~~~~~~~~~~~~~~ */
.popBoxStyle { position: absolute; text-align: center;
visibility:hidden;
z-index: 2000; /* Nairit: Set high so that shows over page contents */
cursor: default;
background-color: #FFFFFF;
border:2px solid;
border-color: #6F7072;
border-radius:6px; /* More gives rounder edges */
box-shadow: 10px 10px 5px #606060; /* Shadow can be used when black overlay is not used on the rest of the page. Or black has low opacity */
filter:alpha(opacity=100); /* Works in IE */
opacity:1.0; /* Works in others. This is for the opacity of the box's background */
padding:3px;
}
.liner { clear:both; height:1px; background-color:#cccccc; }
.spacer { clear:both; height:5px; }
.black { position:absolute; z-index:1999; }
button.close { float:right; margin:5px; cursor:pointer; background-color:#d9534f !important; border-radius:4px !important; color:#fff !important; font-size:21px; font-weight:bold; line-height:1; color:#000000; }
/* ~~~~~~~~~~~~~ Style for the pop box with blackener ends ~~~~~~~~~~~~~~~~~~ */
</style>
<script language="javascript" src="PopBox.js"></script>
<script language="javascript">
var affirm = false; tm_ref=null, cb_func_name=null;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function customConfirm(func,msg)
{
initializePopBox();
popBox.innerHTML = "<table width='100%' cellspacing='0' cellpadding='5' border='0'>" +
"<tr><td>CONFIRM</td>" +
"<td><button type='button' class='close' style='float:right;margin:5px;' onClick='setUserResponse(false);'>x</button></td></tr>" +
"<tr><td colspan='2'><div class='liner'></div></td></tr>" +
"<tr><td colspan='2'>"+msg+"</td></tr>" +
"<tr align='right'>" +
"<td><input type='button' name='b_cancel' value='Cancel' onClick='setUserResponse(false);'></td>" +
"<td><input type='button' name='b_confirm' value='Confirm' onClick='setUserResponse(true);'></td></tr>" +
"</table>";
aWidth = 300; aHeight = 150;
finalizePopBox(50);
cb_func_name = func; // Storing the callback function's name globally
tm_ref = setTimeout(func, 30000); // 60000 milli seconds = 1 minute. 600000 milli seconds = 10 minutes. 900000 milli seconds = 15 minutes
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function handleConfirmResponse()
{
hidePop();
if( affirm )
{
alert("He said Yes"); // Do your work here
}
else
{
alert("He said No"); // Do nothing
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function setUserResponse(respo)
{
affirm = respo;
clearTimeout(tm_ref);
cb_func_name(); // Gets the function name from global and calls it
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
</script>
</head>
<body>
<div id="blackener" class="black"></div>
<div id="popContainer" class="popBoxStyle"></div> <!-- NK: The pop-up appears in this div -->
<input type="button" name="b1" value="Delete" onClick="customConfirm(handleConfirmResponse,'Are you sure?');">
</body>
</html>
支持文件是 PopBox.js :-
var popBox, popBoxStyl, BlackRef, aWidth, aHeight;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function initializePopBox()
{
popBox = document.getElementById("popContainer");
popBoxStyl = document.getElementById("popContainer").style;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function finalizePopBox(tp) // Height from page top
{
popBoxStyl.width = aWidth + "px";
popBoxStyl.height = aHeight + "px";
popBoxStyl.left = parseInt( ( (document.body.clientWidth - aWidth)/2 ) ) + "px";
if(tp)
popBoxStyl.top = tp;
else
popBoxStyl.top = parseInt( ( (document.body.clientHeight - aHeight)/2 ) ) + "px";
popBoxStyl.visibility = "visible"; // Nairit: So they are using .style.visibility = . Not .style.display = which I use normally. Learn this way.
blacken();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function blacken()
{
BlackRef = document.getElementById("blackener");
var ht = document.body.scrollHeight; // document.body.scrollHeight > document.body.offsetHeight > document.body.clientHeight
var wd = document.body.offsetWidth; //alert(ht); alert(wd);
BlackRef.style.left = "0px";
BlackRef.style.top = "0px";
BlackRef.style.height = ht + "px";
BlackRef.style.width = wd + "px";
BlackRef.style.backgroundColor = "#000000";
BlackRef.style.opacity = "0.5";
BlackRef.style.filter = 'alpha(opacity=50)';
BlackRef.style.display = "block";
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function hidePop()
{
popBoxStyl.visibility = "hidden";
BlackRef.style.backgroundColor = "#ffffff";
BlackRef.style.opacity = "1.0";
BlackRef.style.filter = 'alpha(opacity=100)';
BlackRef.style.display = "none";
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我从同一个网页的 2 个不同位置成功调用了 customConfirm()。我必须传递 2 个不同的响应处理程序回调函数名称。在调用 customConfirm() 之前,我必须将其值在相应的回调函数中需要的任何变量设为全局变量。但值得付出努力;-)