Essentially you need to start the timer server-side and then ensure that page refreshes do not alter the client-side countdown status (otherwise you just need to refresh constantly to reset the timer)
To invoke the timer I have the following:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{ TimerStart(); }
}
The TimerStart routine is defined as:
private void TimerStart()
{
if (Session["TimerStart"] == null)
{
Session["TimerStart"] = true;
endDate = new BOL.Date();
}
}
The Session settings is to prevent the page refreshes as mentioned earlier and the class BOL.Date is a class I have created to return the date from my SQL server, it allows me to break the date format down into year, month, day, hour, minute, seconds - if you don't have access to an SQL server you can use various methods to return this information easily.
The user can move to the next item in my list of items displayed without waiting for a full count down by pressing the 'Next Button', the code for this is as follows:
protected void btnNext_Click(object sender, System.EventArgs e)
{
Session["TimerStart"] = null;
TimerStart();
}
To start the javascript timer, the following code needs to be added to the PreRender:
protected override void OnPreRender(EventArgs e)
{
if (Session["TimerStart"] != null)
{
string script = string.Format("javascript: countdownTimer({0}, {1}, {2}, {3}, {4}, {5}, {6});", endDate.Year, endDate.Month, endDate.Day, endDate.Hour, endDate.Minute, endDate.Second, <TimeValueInSeconds>);
ScriptManager.RegisterStartupScript(this, this.GetType(), "TimerStarted", script, true);
}
base.OnPreRender(e);
}
The <TimeValueInSeconds> is exactly what it means - …