加载新页面时计数器变量不持久 [英] Counter variable does not persist when loading a new page

查看:78
本文介绍了加载新页面时计数器变量不持久的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JavaScript中使用计数器变量,例如带有计时器的静态变量。计数器必须每秒减少1。我正在从事在线考试。在回答问题时,同一页面上还会出现新问题。当计数器变量用新问题初始化时(表示新页面),我的问题开始了。计数器变量不会在新页面上持续存在...建议任何解决方案

I want to use counter variable in javascript like static variable with timer. counter must decrease by 1 on every second. I m working on online exam. on answering a question new question comes on the same page. my problem starts when counter variable is initialized with new question(means new page). counter variable does not persists on new page...suggest any solution

<script language="JavaScript">

function ExamTimer() 
{   
if ( typeof ExamTimer.counter == 'undefined' )   
{   
ExamTimer.counter = 30000;  // 30 min
    }
else
{

 ExamTimer.counter = ExamTimer.counter-1;   
    if(ExamTimer.counter<=0)
    {
          alert("exam finish");
     }
setTimeout(ExamTimer, 1000);

  }
window.onload=ExamTimer;
</script>


推荐答案

使用ajax将值从一个页面传递到另一页面。使用会话来保留最后一次剩余时间泰国时间会传递到下一页

Using ajax pass value from one to another page. use session to hold last remainTime thai is passed to next page

<script language="JavaScript">

 function ExamTimer() 
 {   
 if ( typeof ExamTimer.counter == 'undefined' )   
 {   
  ExamTimer.counter = <cfoutput>#session.RemainTime#</cfoutput>;
  }
  else
   {

 ExamTimer.counter = ExamTimer.counter-1; 
 $.get('linktosend.cfm',{counter:ExamTimer.counter},function(responseText)
    { 
// value of ExamTimer.counter send to linktosend.cfm and store in session.RemainTime           
 });  
 if(ExamTimer.counter<=0)
 {
       alert("exam finish");
 }
setTimeout(ExamTimer, 1000);

 }
 window.onload=ExamTimer;

 </script>

这篇关于加载新页面时计数器变量不持久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆