By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,836 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,836 IT Pros & Developers. It's quick & easy.

Timer reset on page back/page forward

P: n/a
I have spent way too much time trying to solve the following problem:

I have a datalist with a timer in the footer template. It works wonderfully
except when the user pages back, then forward, and the counter is reset. I
saw in one of Kevin Spencer's comments not to put the timer in javascript on
the load function, but I have to get the script to run before the page is
rendered, otherwise my timer is not displayed. I have a flag "Hidden Test
Staus" that I try to set at first in page_load, and then reset in the
javascript.

I am getting desperate to understand what to do.

This is my code:

In the code behind:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SqlConnection1.Open()
If IsPostBack = False Then
SetUpQuestions()
RegisterHiddenField("HiddenTestStatus", "Y")
End If

Dim HddenFld As String = Request.Form("HiddenField")
If HddenFld = "1" Then
Me.SaveEmpResponses()
Me.CalculateScore()
Me.DeleteEmployeeResponses()
Response.Redirect("frmSurveyInstruction.aspx", False)
End If
End Sub
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
If IsPostBack = False Then
Dim strScript As String
strScript = "<script language=""javascript"">"
strScript = strScript + "var textBeforeCountdown=""You have "";"
strScript = strScript + "var textAfterCountdown="" to complete this test
...."";"
strScript = strScript + "if (document.Form1.HiddenTestStatus.value=""Y"")
var seconds = 600;"
strScript = strScript + "else var seconds =
document.Form1.HiddenFieldTime.value;"
strScript = strScript + "document.Form1.HiddenTestStatus.value=""N"";"
strScript = strScript + "var secondsAfterAlert = 2;"
strScript = strScript + "var timer;"
strScript = strScript + "function countDown() {"
strScript = strScript + "if (seconds>=60){ "
strScript = strScript + "minutes=""""+seconds/60;"
strScript = strScript + "minutes=minutes.substring(0,1);"
strScript = strScript + "if (minutes!=""1"") mPlural=""s"";"
strScript = strScript + "else mPlural="""";"
strScript = strScript + "minutes2=minutes+"" minute""+mPlural+"" and ""; "
strScript = strScript + "seconds2=seconds-minutes*60;"
strScript = strScript + "if (seconds2!=1) seconds2=seconds2+"" seconds"";"
strScript = strScript + "else seconds2=seconds2+"" second""; "
strScript = strScript +
"timerSpan.innerHTML=textBeforeCountdown+minutes2+ seconds2+textAfterCountdown; "
strScript = strScript + "}"
strScript = strScript + "else{"
strScript = strScript + "if (seconds!=1) seconds2=seconds+"" seconds""; "
strScript = strScript + "else seconds2=seconds+"" second""; "
strScript = strScript +
"timerSpan.innerHTML=textBeforeCountdown+seconds2+ textAfterCountdown; "
strScript = strScript + "}"
strScript = strScript + "if(seconds ==0) {"
strScript = strScript + "stopTimer();"
strScript = strScript + "}"
strScript = strScript + "seconds--;"
strScript = strScript + "document.Form1.HiddenFieldTime.value = seconds;"
strScript = strScript + "if(seconds >=0)
timer=window.setTimeout(""countDown()"",1000);"
strScript = strScript + "}"
strScript = strScript + "function stopTimer() {"
strScript = strScript + "clearTimeout(timer); "
strScript = strScript + "document.Form1.HiddenField.value = ""1"";"
strScript = strScript + "alert(""Your time is up. Please click 'OK' to
continue to the next assessment""); "
strScript = strScript + "timerSpan.innerHTML=""You have completed your
time!!!"";"
strScript = strScript + "document.Form1.submit()"
strScript = strScript + "}"
strScript = strScript + "countDown()"
strScript = strScript + "</script>"
RegisterStartupScript("BeginTest", strScript)
End If
End Sub
In the html:

<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datalist id="dlQuestions" style="Z-INDEX: 101; LEFT: 32px;
POSITION: absolute; TOP: 32px"
runat="server" BorderWidth="1px" GridLines="Both" CellPadding="4"
BackColor="White" BorderStyle="None"
BorderColor="#CC9966" DataKeyField="QuestNum" DataMember="Questions"
Height="328px" Width="950px"
OnItemDataBound="dlQuestions_ItemDataBound">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399"
BackColor="#FFCC66"></SelectedItemStyle>
<HeaderTemplate>
<asp:Label id="Label1" runat="server" Height="42px" Font-Bold="True"
Font-Size="Medium">SHIPLEY INSTITUTE OF LIVING SCALE</asp:Label>
</HeaderTemplate>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<FooterTemplate>
<asp:Button id="btnFinish" onclick="btnFinish_Click" runat="server"
Text="Finish"></asp:Button>
<span id="timerSpan"></span>
</FooterTemplate>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<ItemTemplate>
<P>
<table border="0" cellspacing="10" cellpadding="5" bordercolor="black">
<tr>
<td width='750'>
<asp:Label id=lblQuestNum runat="server" Height="30px"
Width="100px" Font-Bold="False" Text='<%# "(" &
DataBinder.Eval(Container.DataItem,"QuestNum") & " of 40) " %>'
Font-Size="XX-Small">
</asp:Label>
<asp:Label id=lblQuestDescr runat="server" Height="30px"
Width="120px" Font-Bold="True" Text='<%#
DataBinder.Eval(Container.DataItem,"QuestDescr") %>' Font-Size="XX-Small">
</asp:Label>
<asp:RadioButtonList id="Rbl" runat="server" Width="500px"
Height="30px" Font-Size="XX-Small" DataTextField="ResponseDescr"
DataValueField="ResponseNum" RepeatLayout="Flow"
RepeatDirection="Horizontal"></asp:RadioButtonList>
</td>
</tr>
</table>
</P>
</ItemTemplate>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC"
BackColor="#990000"></HeaderStyle>
</asp:datalist>
<asp:label id="lblErrorMessage" style="Z-INDEX: 102; LEFT: 200px;
POSITION: absolute; TOP: 8px"
runat="server" ForeColor="Red" Font-Bold="True"></asp:label>
<input id="HiddenField" type="hidden" name="HiddenField" runat="server">
<input id="HiddenFieldTime" type="hidden" name="HiddenFieldTime"
runat="server">
</form>
Jan 24 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Your question is confusing to read (to me), and I didn't follow it well, but
I can tell you why the JavaScript timer isn't working. You're using the
JavaScript setTimeout function to call your countDown function. The
setTimeout function only executes one time. You need to use the JavaScript
setInterval function if you want to continually call the countDown function.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"jmgro" <jm***@discussions.microsoft.com> wrote in message
news:A2**********************************@microsof t.com...
I have spent way too much time trying to solve the following problem:

I have a datalist with a timer in the footer template. It works
wonderfully
except when the user pages back, then forward, and the counter is reset.
I
saw in one of Kevin Spencer's comments not to put the timer in javascript
on
the load function, but I have to get the script to run before the page is
rendered, otherwise my timer is not displayed. I have a flag "Hidden Test
Staus" that I try to set at first in page_load, and then reset in the
javascript.

I am getting desperate to understand what to do.

This is my code:

In the code behind:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SqlConnection1.Open()
If IsPostBack = False Then
SetUpQuestions()
RegisterHiddenField("HiddenTestStatus", "Y")
End If

Dim HddenFld As String = Request.Form("HiddenField")
If HddenFld = "1" Then
Me.SaveEmpResponses()
Me.CalculateScore()
Me.DeleteEmployeeResponses()
Response.Redirect("frmSurveyInstruction.aspx", False)
End If
End Sub
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
If IsPostBack = False Then
Dim strScript As String
strScript = "<script language=""javascript"">"
strScript = strScript + "var textBeforeCountdown=""You have "";"
strScript = strScript + "var textAfterCountdown="" to complete this test
..."";"
strScript = strScript + "if (document.Form1.HiddenTestStatus.value=""Y"")
var seconds = 600;"
strScript = strScript + "else var seconds =
document.Form1.HiddenFieldTime.value;"
strScript = strScript + "document.Form1.HiddenTestStatus.value=""N"";"
strScript = strScript + "var secondsAfterAlert = 2;"
strScript = strScript + "var timer;"
strScript = strScript + "function countDown() {"
strScript = strScript + "if (seconds>=60){ "
strScript = strScript + "minutes=""""+seconds/60;"
strScript = strScript + "minutes=minutes.substring(0,1);"
strScript = strScript + "if (minutes!=""1"") mPlural=""s"";"
strScript = strScript + "else mPlural="""";"
strScript = strScript + "minutes2=minutes+"" minute""+mPlural+"" and ""; "
strScript = strScript + "seconds2=seconds-minutes*60;"
strScript = strScript + "if (seconds2!=1) seconds2=seconds2+"" seconds"";"
strScript = strScript + "else seconds2=seconds2+"" second""; "
strScript = strScript +
"timerSpan.innerHTML=textBeforeCountdown+minutes2+ seconds2+textAfterCountdown;
"
strScript = strScript + "}"
strScript = strScript + "else{"
strScript = strScript + "if (seconds!=1) seconds2=seconds+"" seconds""; "
strScript = strScript + "else seconds2=seconds+"" second""; "
strScript = strScript +
"timerSpan.innerHTML=textBeforeCountdown+seconds2+ textAfterCountdown; "
strScript = strScript + "}"
strScript = strScript + "if(seconds ==0) {"
strScript = strScript + "stopTimer();"
strScript = strScript + "}"
strScript = strScript + "seconds--;"
strScript = strScript + "document.Form1.HiddenFieldTime.value = seconds;"
strScript = strScript + "if(seconds >=0)
timer=window.setTimeout(""countDown()"",1000);"
strScript = strScript + "}"
strScript = strScript + "function stopTimer() {"
strScript = strScript + "clearTimeout(timer); "
strScript = strScript + "document.Form1.HiddenField.value = ""1"";"
strScript = strScript + "alert(""Your time is up. Please click 'OK' to
continue to the next assessment""); "
strScript = strScript + "timerSpan.innerHTML=""You have completed your
time!!!"";"
strScript = strScript + "document.Form1.submit()"
strScript = strScript + "}"
strScript = strScript + "countDown()"
strScript = strScript + "</script>"
RegisterStartupScript("BeginTest", strScript)
End If
End Sub
In the html:

<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datalist id="dlQuestions" style="Z-INDEX: 101; LEFT: 32px;
POSITION: absolute; TOP: 32px"
runat="server" BorderWidth="1px" GridLines="Both" CellPadding="4"
BackColor="White" BorderStyle="None"
BorderColor="#CC9966" DataKeyField="QuestNum" DataMember="Questions"
Height="328px" Width="950px"
OnItemDataBound="dlQuestions_ItemDataBound">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399"
BackColor="#FFCC66"></SelectedItemStyle>
<HeaderTemplate>
<asp:Label id="Label1" runat="server" Height="42px" Font-Bold="True"
Font-Size="Medium">SHIPLEY INSTITUTE OF LIVING SCALE</asp:Label>
</HeaderTemplate>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<FooterTemplate>
<asp:Button id="btnFinish" onclick="btnFinish_Click" runat="server"
Text="Finish"></asp:Button>
<span id="timerSpan"></span>
</FooterTemplate>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<ItemTemplate>
<P>
<table border="0" cellspacing="10" cellpadding="5"
bordercolor="black">
<tr>
<td width='750'>
<asp:Label id=lblQuestNum runat="server" Height="30px"
Width="100px" Font-Bold="False" Text='<%# "(" &
DataBinder.Eval(Container.DataItem,"QuestNum") & " of 40) " %>'
Font-Size="XX-Small">
</asp:Label>
<asp:Label id=lblQuestDescr runat="server" Height="30px"
Width="120px" Font-Bold="True" Text='<%#
DataBinder.Eval(Container.DataItem,"QuestDescr") %>' Font-Size="XX-Small">
</asp:Label>
<asp:RadioButtonList id="Rbl" runat="server" Width="500px"
Height="30px" Font-Size="XX-Small" DataTextField="ResponseDescr"
DataValueField="ResponseNum" RepeatLayout="Flow"
RepeatDirection="Horizontal"></asp:RadioButtonList>
</td>
</tr>
</table>
</P>
</ItemTemplate>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC"
BackColor="#990000"></HeaderStyle>
</asp:datalist>
<asp:label id="lblErrorMessage" style="Z-INDEX: 102; LEFT: 200px;
POSITION: absolute; TOP: 8px"
runat="server" ForeColor="Red" Font-Bold="True"></asp:label>
<input id="HiddenField" type="hidden" name="HiddenField" runat="server">
<input id="HiddenFieldTime" type="hidden" name="HiddenFieldTime"
runat="server">
</form>

Jan 24 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.