• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

Elapsed Time on the site

I've got a quiz that has 10 pages (each page has a question).
What I need is to display the "elapsed time". Pretty much the amount of time the person spent on the site. (I want to be able to add an aspLabel where I want the time to display on the site)

It would only refresh the "time label" when the person clicks "next" to go to another question.
Then on the final page it would Stop the timer (or whatever you call it).

Please look at these print-screen of the quiz. One of them is a question screen and the other is what the finish page with the score looks like. (I circled the area where I want the "Elapsed Time" to appear).

http://www.myviahealthdrink.com/questionpage.gif
http://www.myviahealthdrink.com/finishPage.gif
0
da_question
Asked:
da_question
  • 8
  • 4
1 Solution
 
da_questionAuthor Commented:
oh, one more thing... I'm not a programmer :)
0
 
da_questionAuthor Commented:
here's a piece of the code that displays the result page.
I want to be able to add one of these "<asp:literal id="timeSpent" runat="server" />" to the body of my html.

<!-- start header -->

<%
        Dim answers As String = WizardStep.FormatMergeField("txtResultsDisplay")
        Dim a As String() = answers.Split(",")

        Dim rightanswers As Decimal = 0
        Dim questions As Decimal = Convert.ToDecimal(a.GetUpperBound(0) + 1)
        Dim x As Int32
        For x = 0 To questions - 1
            rightanswers += Convert.ToDecimal(a(x))
        Next

        Dim yourScore As Decimal = (rightanswers / questions) * 100

If  yourScore > 80 then
     me.lit1.text = "<b> Some Bold</b>whatever other html you want in the page."
Else
     me.lit1.text = "<b> Some Bold text</b>whatever other html you want in the page for a score not above 79."
End if
        Dim result As String = String.Format("{0:N}", yourScore)
       
        txtResultsDisplay.Text = result
        txtBox.Text = String.Format("{0:0}", rightanswers)  + " of " + String.Format("{0:0}", questions)

%>


Points: <b><asp:label id="txtBox" runat="server" /></b><BR>Your Competency: <b><asp:label id="txtResultsDisplay" runat="server" /> %</b>
<br>
<asp:literal id="lit1" runat="server" />
0
 
stengeljCommented:
Ok, I'll try and make this as easy as I can...


For your start page...
------------------------------------------------------------------------------------------------------
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<html>
<head>
<script runat="server">
    Sub Page_Load(Src As Object, E As EventArgs)
            'Create a session variable to hold the start time
            Session("myStartTime") = Now()
    End Sub
</script>
</head>
<body>
...
</html>

For your finish page (and any in-between pages)...Returns the amount of time that had elapsed since starting the quiz.
------------------------------------------------------------------------------------------------------
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" debug="true" %>
<html>
<head>
<script runat="server">
    Sub Page_Load(Src As Object, E As EventArgs)
        Dim d1 = Session("myStartTime")
        Dim d2 = Now()
        lit_myTime.Text = DateDiff(DateInterval.Minute,CDate(d1),CDate(d2)) & " min"
    End Sub
</script>
</script>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form runat="server">
    <asp:literal runat="server" ID="lit_myTime" />
</form>
</body>
</html>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
da_questionAuthor Commented:
It worked Perfectly... just one thing, is there any way you can display the "seconds"?? lets say it only took them half of a minute to complete the quiz, I would like it to say: "0.30 min"

Is this possible?? I Greatly appreciate it :)
0
 
da_questionAuthor Commented:
Same goes for all the pages in between... so if they're on question number 5 and it only took them "0.25 min" until that point, I'd like it to display "0.25 min" (on the bottom right of the printscreen shown above)
0
 
da_questionAuthor Commented:
One more thing... why does it display what looks to be fractions of a minute sometimes??
view this link: http://www.myviahealthdrink.com/etime.gif
0
 
stengeljCommented:
Instead of "DateInterval.Minute", you can put in some formatting options that let you display it however you want.  I'll look for some links that will show you all your options.

As for your second question, that's very weird...

Perhaps a different time format will help.  I'll see what I can dig up.
0
 
da_questionAuthor Commented:
I appreciate it :)
0
 
stengeljCommented:
Ok, try this for your Page_Load script on your subsequent pages. I gave you two examples to show you have to do some custom formatting.

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" debug="true" %>
<html>
<head>
<script runat="server">
    Sub Page_Load(Src As Object, E As EventArgs)
        Dim d1 = CDate(Session("myStartTime"))
        Dim d2 = Now()
            Dim ts as TimeSpan = TimeSpan.FromSeconds(DateDiff(DateInterval.Second,CDate(d1),CDate(d2)))
            lit_MyTime1.Text = ts.hours.ToString & " hrs. " & ts.Minutes.ToString & " min. " & ts.seconds.ToString & " sec."
            lit_MyTime2.Text = Math.Round(DateDiff(DateInterval.Second,d1,d2)/60,2) & " min"
    End Sub
</script>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form runat="server">
    Example 1:
      <asp:literal runat="server" ID="lit_myTime1" /><br>
      Example 2:
      <asp:literal runat="server" ID="lit_myTime2" /><br>
</form>
</body>
</html>

If you get a really weird number of minutes (like what you posted), it is probably because your session expired or you visited a secondary page before going to the start page. If you don't go to the start page first the start time value never gets set.  

I am using a session variable to hold the start time and it expires after 20 minutes, if yo close the browser, and if you leave the site.  I didn't think this would be an issue for users taking the quiz but it could give you weird results during development.  You could also store the start time in a cookie but then you have to be sure that they can accept cookies.
0
 
da_questionAuthor Commented:
Perfect!!! You're the Best :)
0
 
stengeljCommented:
Thank you. Glad it works for you.

I did notice one thing...
You can change "DateDiff(DateInterval.Second,CDate(d1),CDate(d2)))" to "DateDiff(DateInterval.Second,d1,d2))".
I was testing some stuff and forgot to clean that up.
0
 
da_questionAuthor Commented:
Thanks -- I'll do that!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now