2011-04-12 81 views
0

嗨 我已经创建了在线测验。我添加了倒数计时器,问题的标签& radiobuttonlist的答案和下一个问题的下一个按钮。我有定时器的代码,但是当我点击下一个按钮时,这个定时器会重新开始,因为我想倒计时器的整个问题(测验)。 倒计时代码(JavaScript)的如下:关于(倒计时)在asp.net中应用的计时器c#

var hour=0; //specify hours for counter 
var min= '<%= Session["timer"] %>';  // specify minutes 

var second = '<%= Session["second"] %>'; // specify the seconds 
var lab = 'cd'; // id of the entry on the page where the counter(for question) is to be inserted & cd is span id in aspx page where i am displaying countdown timer 

function start() 
{ 
    displayCountdown(setCountdown(hour,min,second),lab); 
} 
loaded(lab,start); 
var pageLoaded = 0; 
window.onload = function() {pageLoaded = 1;} 
function loaded(i,f) 
{ 
    if (document.getElementById && document.getElementById(i) != null) 
     f(); 
    else if (!pageLoaded) 
     setTimeout('loaded(\''+i+'\','+f+')',100); 
} 
function setCountdown(hour,min,second) 
{ 
    if(hour>0) 
    min=min*hour*60; 
    c = setC(min,second); 
return c; 
} 
function setC(min,second) 
{ 
if(min>0) 
second=min*60*second; 
return Math.floor(second); 
} 
function displayCountdown(countdn,cd) 
{ 
    if (countdn < 0) 
    { 
     document.getElementById(cd).innerHTML = "Sorry, you are too late."; 
     __doPostBack('__Page'); 
    } 
    else 
    { 
     var secs = countdn % 60; 
     if (secs < 10) 
      secs = '0'+secs; 
     var countdn1 = (countdn - secs)/60; 
     var mins = countdn1 % 60; 
     if (mins < 10) 
      mins = '0'+mins; 
     countdn1 = (countdn1 - mins)/60; 
     var hours = countdn1 % 24; 
     document.getElementById(cd).innerHTML = hours+' : '+mins+' : '+secs; 
     setTimeout('displayCountdown('+(countdn-1)+',\''+cd+'\');',999); 
    } 
} 
+0

希望你知道,JavaScript可以轻易无法...确保在服务器端进行验证。 – 2011-04-12 13:09:43

回答

0

你必须保持在相对的“开始”时间“现在”时间的参考和测验的时间。所以,你会从开始时间减去“现在”的时间。

protected int HoursDuration { 
     get { 
      if (Session["HoursDuration"] == null) { Session["HoursDuration"] = 0; } 
      return Convert.ToInt32(Session["HoursDuration"]); 
     } 
     set { Session["HoursDuration"] = value; } 
    } 

    protected int MinutesDuration { 
     get { 
      if (Session["MinutesDuration"] == null) { Session["MinutesDuration"] = 0; } 
      return Convert.ToInt32(Session["MinutesDuration"]); 
     } 
     set { Session["MinutesDuration"] = value; } 
    } 

    protected int SecondsDuration { 
     get { 
      if (Session["SecondsDuration"] == null) { Session["SecondsDuration"] = 0; } 
      return Convert.ToInt32(Session["SecondsDuration"]); 
     } 
     set { Session["SecondsDuration"] = value; } 
    } 

    protected int HoursLeft { 
     get { 
      return (this.EndTime - this.BeginTime).Hours; 
     } 
    } 

    protected int MinutesLeft { 
     get { 
      return (this.EndTime - this.BeginTime).Minutes; 
     } 
    } 

    protected int SecondsLeft { 
     get { 
      return (this.EndTime - this.BeginTime).Seconds; 
     } 
    } 

    protected DateTime EndTime { 
     get { 
      if (Session["EndTime"] == null) { Session["EndTime"] = DateTime.Now; } 
      return Convert.ToDateTime(Session["EndTime"]); 
     } 
     set { ViewState["EndTime"] = value; } 
    } 

    protected DateTime BeginTime { 
     get { 
      if (Session["BeginTime"] == null) { Session["BeginTime"] = DateTime.Now; } 
      return Convert.ToDateTime(Session["BeginTime"]); 
     } 
     set { ViewState["BeginTime"] = value; } 
    } 

    protected override void OnInit(EventArgs e) { 
     this.BeginTime = DateTime.Now; // Present time 
     if (!IsPostBack) { 
      // The countdown 
      this.HoursDuration = 0; 
      this.MinutesDuration = 10; 
      this.SecondsDuration = 0; 
      // Only on !postback, you set up when it ends 
      this.EndTime = this.BeginTime.AddHours(this.HoursDuration) 
             .AddMinutes(this.MinutesDuration) 
             .AddSeconds(this.SecondsDuration); 
     } 
     base.OnInit(e); 
    } 

然后,在你的javascript中,调用HoursLeft,MinutesLeft和secondeLeft。我认为这应该工作。