【问题标题】:Controlling a Javascript Timer from Another Page?从另一个页面控制 Javascript 计时器?
【发布时间】:2012-03-13 12:18:07
【问题描述】:

我正在尝试为我的应用程序创建一个自动注销页面。我正在使用 Javascript 来处理带有经典 asp 的计时器。我有三个页面是 js 脚本页面,我调用 js 文件并使用 OnLoad() 启动计时器的主页。 4 秒后会弹出一个窗口,要求用户继续该会话。但是当窗口弹出并且我在 logout_alert.asp 页面上单击“是”时,它不会更新主页上的计时器。如何让弹出窗口更新计时器?以下是脚本。

提前感谢您的帮助,

弗兰克 G.

我有三页。对于此示例,第 1 页、第 2 页和第 3 页。

第 1 页 = logout_timeout.js


这是该页面的脚本。

<!-- Begin

var c=0;
var t;

function ClockCount(){

c=c+1;
    //Show the clock value on the home page text field.
    document.timerform.clock.value = c;

if (c == 7) {
    //The user never responded so we will kill the session and log them out.
    alert("Your session has timed out you are logged out.");
    window.location.href = "logout.asp"
}
else if (c == 4) { 

    //We will popup a window to let the user know there about to be logged out.
    //This will give the user a chance to keep the session alive.
    logoutalert = window.open("logout_alert.asp", "logoutalert",    "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=420,height=145,left = 465,top = 250");
}

 //The timer will go round and round while recalling this function.
 t = setTimeout("ClockCount()",1000);

 }

 //Call to start the timer.
 function StartClock() {
 ClockCount();
 }

 //Called to stop the timer.
 function StopClock() {
 c=0 //Clear the timer.
 clearTimeout(t); //Stop it.
 }

 //  End -->

第 2 页 = 主页.asp


这个页面我调用 logout_timeout.js 文件。

 <script src="logout_timeout.js" type="text/javascript"></script>
 <body OnLoad="StartClock()">
 This will show our clock while its processing.
 <form id="timerform" name="timerform" method="post" action="">
   <input type="text" name="clock" id="clock" />
 </form>
 Used now just for testing!
   <input type="submit" name="continue" id="continue" value="START" onClick="StartClock()" />
   <input type="submit" name="continue" id="continue" value="STOP" onClick="StopClock()" />

第 3 页 = logout_alert.asp


这个页面会弹出来通知用户他们即将被注销。

 <script src="logout_timeout.js" type="text/javascript"></script>
 <body>
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr>
 <td class="Thread_Title">SESSION TIMEOUT  Alert!</td>
 </tr>
 <tr>
  <td width="85%" class="DataRows">Your session is about to timeout. Would you like to continue using WOTTS?</td>
 </tr>
 <tr>
  <td></td>
 </tr>
 <tr>
  <td></td>
 </tr>
 <tr>
  <td><form id="timerform" name="timerform" method="post" action="">
 <input type="text" name="clock" id="clock" />
 </form></td>
 </tr>
 <tr>
  <td>
   <input type="submit" name="continue" id="continue" value="START" onClick="StartClock()" />
 <input type="submit" name="continue" id="continue" value="STOP" onClick="StopClock()" />   </td>
  </tr>
  </table>

【问题讨论】:

    标签: javascript asp-classic


    【解决方案1】:

    您缺少一块拼图:对另一个窗口的引用。某些安全限制沙箱分离窗口对象,除非它们是从现有窗口产生的。如果您使用 JavaScript 的 window.open () 函数,您的新窗口将打开,并带有打开它的父窗口的句柄。然后,在脚本中,您可以利用该句柄来操作对象和行为,并在父窗口中执行功能。这将这一切联系在一起。

    这里有很多例子:

    Can I pass a JavaScript variable to another browser window?

    【讨论】:

    • 您好,感谢您的帮助。包含计时器的窗口位于框架内。这是该框架的代码: 并且该框架称为“autologout”,但计时器脚本本身位于 js 文件中。我在两个窗口之间共享该 js 文件,但将其插入。但是,如果我在该页面本身上有一个按钮,我只能重置计时器。例如,如果我在计时器页面上放置一个按钮来停止它的工作时间,但如果我告诉弹出窗口告诉计时器停止它不会。有什么建议吗?谢谢!
    • 您可以使用 window.opener, window.opener.frames[], top.opener.myFrameName.window.myFunction() ... 或者,在父窗口(或框架)上调用函数您可以在子窗口的 JavaScript 中创建一个函数,当您将引用传递给打开器时执行此操作,例如“stopRemoteTimer (top.opener.myFrameName);”您将在其中以这种方式处理父窗口中的实际功能。当 JS 在其自己的沙箱中运行时,您必须在一个或另一个窗口的上下文中进行所有计算和操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 2014-01-22
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    相关资源
    最近更新 更多