开发者

Regarding (count down) timer for application in asp.net c#

开发者 https://www.devze.com 2023-02-25 12:28 出处:网络
Hi I have created online quiz. I have added Count-down timer,label for question & radiobuttonlist for answers and next button for next question. I have code for timer but this timer gets start aga

Hi I have created online quiz. I have added Count-down timer,label for question & radiobuttonlist for answers and next button for next question. I have code for timer but this timer gets start again when i click next button as i want count down timer for whole questions(Quiz). Count down timer code(Javascript) is as follows:

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


You must keep a reference on "present" time relatively of the "start" time and the duration of the quiz. So, you would substract "present" time from start time.

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

Then, in you javascript, call the HoursLeft, MinutesLeft and secondeLeft. I think this should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消