关于(倒计时)计时器在asp.net中的应用程序c#

时间:2011-04-12 12:27:56

标签: asp.net

您好 我创建了在线测验。我添加了倒计时器,标签用于问题& radiututtonlist for answers和下一个问题的下一个按钮。我有计时器的代码,但当我点击下一个按钮时,这个计时器再次启动,因为我想要倒计时器整个问题(测验)。 倒计时器代码(Javascript)如下:

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);
    }
}

1 个答案:

答案 0 :(得分: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。我认为这应该有用。