Timer control

Discussion in 'ASP .Net' started by arunkumar_m2001@yahoo.com, Jan 5, 2006.

  1. Guest

    Can Somebody help me to code a timed test. Actually I want to implement
    a timer in a web application which will pop-up an alert automatically
    by refreshing the web page after a specific time.

    Thanks
    Arun
     
    , Jan 5, 2006
    #1
    1. Advertising

  2. Guest

    Correction:

    Can Somebody help me to code a timed test. Actually I want to implement

    a timer in a web application which will pop-up an alert automatically
    by refreshing the web page after a specific time in ASP.NET.


    Thanks
    Arun
     
    , Jan 5, 2006
    #2
    1. Advertising

  3. That would be a client-side timer, which can be achieved with an HTML META
    Refresh tag or JavaScript (using the setTimeout() method).

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    You can lead a fish to a bicycle,
    but it takes a very long time,
    and the bicycle has to *want* to change.

    <> wrote in message
    news:...
    > Correction:
    >
    > Can Somebody help me to code a timed test. Actually I want to implement
    >
    > a timer in a web application which will pop-up an alert automatically
    > by refreshing the web page after a specific time in ASP.NET.
    >
    >
    > Thanks
    > Arun
    >
     
    Kevin Spencer, Jan 5, 2006
    #3
  4. Guest

    Kevin,
    Thanks for your reply. I used the meta tag to refresh the page.
    My application is a timed test one. I need to capture the current time
    when the user access the application on page load. And after an hour
    the application should pop-up an alert saying time expired by comparing
    the current time with the time when the user started the test.

    Refresh tag just refreshes the page, but I couldn't compare the times
    because when the page reloads the current time changes to the latest
    time which is obvious, can you suggest me a solution for this.

    Thanks again
    Arun
     
    , Jan 5, 2006
    #4
  5. Use the JavaScript setTimeout() function to launch the popup window. This
    function takes 2 parameters: a string that contains the function call, and
    the number of milliseconds to wait. A second is 1000 milliseconds, and there
    are 3600 seconds in an hour, so you want to set the milliseconds to 3600000.
    Example:

    <script type="text/javascript"><!--
    setTimeout("loadPopupFunction()", 360000);
    // --></script>

    Note that this doesn't need to know the time. It will fire 1 hour after the
    page loads. If the page reloads at this time, the timer is restarted when
    the page returns to the client.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    You can lead a fish to a bicycle,
    but it takes a very long time,
    and the bicycle has to *want* to change.

    <> wrote in message
    news:...
    > Kevin,
    > Thanks for your reply. I used the meta tag to refresh the page.
    > My application is a timed test one. I need to capture the current time
    > when the user access the application on page load. And after an hour
    > the application should pop-up an alert saying time expired by comparing
    > the current time with the time when the user started the test.
    >
    > Refresh tag just refreshes the page, but I couldn't compare the times
    > because when the page reloads the current time changes to the latest
    > time which is obvious, can you suggest me a solution for this.
    >
    > Thanks again
    > Arun
    >
     
    Kevin Spencer, Jan 5, 2006
    #5
  6. Guest

    Thanks Kevin,

    It really helped. Now I could pop up an alert after the specific
    time.Now it's on the client side. But,I want to execute server side
    script when the time expires.
    For eg: If the time expires in the test the alert should appear,no
    matter what the user does whether he clicks ok in the alert pop-up or
    ignores it, the server side script should execute and submit the
    answers to the database.

    In other words after the time expiration I need to trigger an event
    without clicking any buttons(or controls).

    Thanks again
    I appreciate your help
    Arun
     
    , Jan 5, 2006
    #6
  7. Hi Arun,

    All you need to do is programmatically (via JavaScript) submit the form.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    You can lead a fish to a bicycle,
    but it takes a very long time,
    and the bicycle has to *want* to change.

    <> wrote in message
    news:...
    > Thanks Kevin,
    >
    > It really helped. Now I could pop up an alert after the specific
    > time.Now it's on the client side. But,I want to execute server side
    > script when the time expires.
    > For eg: If the time expires in the test the alert should appear,no
    > matter what the user does whether he clicks ok in the alert pop-up or
    > ignores it, the server side script should execute and submit the
    > answers to the database.
    >
    > In other words after the time expiration I need to trigger an event
    > without clicking any buttons(or controls).
    >
    > Thanks again
    > I appreciate your help
    > Arun
    >
     
    Kevin Spencer, Jan 5, 2006
    #7
  8. Guest

    Thanks Kevin,

    Is it possible to execute the server side script via javascript
    submit.Can you help me with some sample code/

    Thanks for all your help
    Arun
     
    , Jan 5, 2006
    #8
  9. Guest

    Thanks Kevin,

    Is it possible to execute the server side script via javascript
    submit.Can you help me with some sample code/

    Thanks for all your help
    Arun
     
    , Jan 5, 2006
    #9
  10. The Page executes every time the form is submitted. I'm not sure what you're
    having trouble with.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    You can lead a fish to a bicycle,
    but it takes a very long time,
    and the bicycle has to *want* to change.

    <> wrote in message
    news:...
    > Thanks Kevin,
    >
    > Is it possible to execute the server side script via javascript
    > submit.Can you help me with some sample code/
    >
    > Thanks for all your help
    > Arun
    >
     
    Kevin Spencer, Jan 6, 2006
    #10
  11. Guest

    Hi Kevin:

    The page executes successfully. But when it executes I need to check if
    the session is valid in the code behind and do some action event.

    Thanks
    Arun
     
    , Jan 6, 2006
    #11
  12. Guest

    Kevin,

    Basically when the time expires I should abandon the session and also I
    need to write in a way that it checks the session and do some action
    events(i.e like redirecting to different page etc..) I dont see how I
    can do session abandon in javascript

    Thanks
    Arun
     
    , Jan 6, 2006
    #12
  13. You don't abandon Session on the client. You submit the form using
    JavaScript and do your work on the server when the Page posts.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    You can lead a fish to a bicycle,
    but it takes a very long time,
    and the bicycle has to *want* to change.

    <> wrote in message
    news:...
    > Kevin,
    >
    > Basically when the time expires I should abandon the session and also I
    > need to write in a way that it checks the session and do some action
    > events(i.e like redirecting to different page etc..) I dont see how I
    > can do session abandon in javascript
    >
    > Thanks
    > Arun
    >
     
    Kevin Spencer, Jan 6, 2006
    #13
  14. Guest

    Kevin,

    In that case I need to compare the time.. like the current time and the
    test start time which makes the setTimeout property in Javascript of no
    use.

    My exact thing is I need to call a function that inserts a record into
    the database when the page loads only when the Time Expires. So again I
    need to compare the timings as discussed before levaing the setTimeout
    property of no use.

    Thanks for you help again
    Arun
     
    , Jan 6, 2006
    #14
  15. Your client-side script is going to submit the form after a specific time
    interval. If you need to distinguish between a client-side Event causing the
    PostBack, and your client-side timer posting back, add a hidden form field
    to the page, and have your script insert a value into it prior to submitting
    the form. If the hidden form field has a value, it was submitted as a result
    of the timer. This is similar to the way a Server Control raises a
    server-side event from a client-side event. In fact, you could even create a
    Server Control that does all this. But unless you want to re-use it in
    another app, this would be simpler to accomplish.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    You can lead a fish to a bicycle,
    but it takes a very long time,
    and the bicycle has to *want* to change.

    <> wrote in message
    news:...
    > Kevin,
    >
    > In that case I need to compare the time.. like the current time and the
    > test start time which makes the setTimeout property in Javascript of no
    > use.
    >
    > My exact thing is I need to call a function that inserts a record into
    > the database when the page loads only when the Time Expires. So again I
    > need to compare the timings as discussed before levaing the setTimeout
    > property of no use.
    >
    > Thanks for you help again
    > Arun
    >
     
    Kevin Spencer, Jan 7, 2006
    #15
  16. Guest

    Hi Kevin:

    Thanks for the reply. It sounds good.But in the quiz, when the user
    clicks next button, the page reloads everytime when it's clicked,so
    during the postback the Javascript loadpopupfunction() executes
    evreytime starting the timer for every single click.That's where I have
    problems, if there's something like notpostback method as in server
    side script it will be really helpful.

    Is there anyway I can have a method that could start the timer only
    once.

    Thanks again
    Arun
     
    , Jan 10, 2006
    #16
  17. I believe I outlined a way to do this with a hidden form field.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    You can lead a fish to a bicycle,
    but it takes a very long time,
    and the bicycle has to *want* to change.

    <> wrote in message
    news:...
    > Hi Kevin:
    >
    > Thanks for the reply. It sounds good.But in the quiz, when the user
    > clicks next button, the page reloads everytime when it's clicked,so
    > during the postback the Javascript loadpopupfunction() executes
    > evreytime starting the timer for every single click.That's where I have
    > problems, if there's something like notpostback method as in server
    > side script it will be really helpful.
    >
    > Is there anyway I can have a method that could start the timer only
    > once.
    >
    > Thanks again
    > Arun
    >
     
    Kevin Spencer, Jan 10, 2006
    #17
  18. Guest

    I don't really understand what you said.
    Actually in my codebehind server script - I get the start time and
    store it in a session variable
    Sub PageLoad()
    ** During load and not postback
    If Not IsPostBack Then
    Session("ExpTime") = Now().AddMinutes(5)
    lblTimeDisplay.Text = "This is a 5 minute quiz.Your quiz
    will expire by " & String.Format("{0:t}", Now().AddMinutes(5))
    end if

    ** During load and postback

    If Now() > Session("ExpTime") Then
    SubmitAnswers()
    End If

    End Sub

    The above code works fine, and the answer gets submitted perfectly when
    the time is expired and a user clicks a submit,next question or
    previous question(any click event that causes postback)

    In my Javascript code
    <html>
    <head>
    <script language="javascript">
    function loadPopupFunction()
    {
    alert("Time Expired")
    document.Form1.submit();
    }
    </script>
    </HEAD>
    <body onload="setTimeout('loadPopupFunction()',300000)"
    MS_POSITIONING="GridLayout">


    The timeout works fine but it will restart everytime when the page
    loads.I need to get the alert exactly when the time expires which on
    click will eventually submit the form.

    The hidden field that you said I couldn't properly understand.
    Sorry!
    Thanks for all yuor help
    Arun
     
    , Jan 10, 2006
    #18
  19. Okay, you only want to have this timeout execute once, right? Well, first of
    all, you don't need to do any checking on the server. The client side will
    submit the form when the time is up. Second, since you only want to start
    the timer once, when the test starts, you don't want to put the JavaScript
    into the onload attribute of the body. When you are ready to start the test,
    from the server side, add the timeout JavaScript to the page using
    Page.RegisterStartupScript(). Only do this when the user is ready to start
    the test. Put a hidden form field into the form, and set it to a blank
    value. The client-side script will put a string into the hidden form field
    before it submits the form. All you have to do on the server side is check
    the hidden form field whenever the Page is Posted Back. If the value of the
    hidden form field is not blank, the timer posted the Page back, so you can
    submit the answers.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    You can lead a fish to a bicycle,
    but it takes a very long time,
    and the bicycle has to *want* to change.

    <> wrote in message
    news:...
    >I don't really understand what you said.
    > Actually in my codebehind server script - I get the start time and
    > store it in a session variable
    > Sub PageLoad()
    > ** During load and not postback
    > If Not IsPostBack Then
    > Session("ExpTime") = Now().AddMinutes(5)
    > lblTimeDisplay.Text = "This is a 5 minute quiz.Your quiz
    > will expire by " & String.Format("{0:t}", Now().AddMinutes(5))
    > end if
    >
    > ** During load and postback
    >
    > If Now() > Session("ExpTime") Then
    > SubmitAnswers()
    > End If
    >
    > End Sub
    >
    > The above code works fine, and the answer gets submitted perfectly when
    > the time is expired and a user clicks a submit,next question or
    > previous question(any click event that causes postback)
    >
    > In my Javascript code
    > <html>
    > <head>
    > <script language="javascript">
    > function loadPopupFunction()
    > {
    > alert("Time Expired")
    > document.Form1.submit();
    > }
    > </script>
    > </HEAD>
    > <body onload="setTimeout('loadPopupFunction()',300000)"
    > MS_POSITIONING="GridLayout">
    >
    >
    > The timeout works fine but it will restart everytime when the page
    > loads.I need to get the alert exactly when the time expires which on
    > click will eventually submit the form.
    >
    > The hidden field that you said I couldn't properly understand.
    > Sorry!
    > Thanks for all yuor help
    > Arun
    >
     
    Kevin Spencer, Jan 10, 2006
    #19
  20. Guest

    Thanks Kevin

    Thanks for all your help.I 'll try this way.It sounds great.

    Arun
     
    , Jan 12, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kelsang Wangchuk

    System.Timers.Timer vs. System.Threading.Timer

    Kelsang Wangchuk, Jul 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    756
    Kelsang Wangchuk
    Jul 31, 2003
  2. Kevin Spencer

    Re: Question concerning Timer Control

    Kevin Spencer, Sep 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    413
    Kevin Spencer
    Sep 21, 2004
  3. Replies:
    1
    Views:
    1,677
    Steve C. Orr [MVP, MCSD]
    Feb 22, 2005
  4. Simon

    page timer, or redirect timer

    Simon, Nov 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    11,261
    Bruce Barker
    Nov 4, 2005
  5. Replies:
    8
    Views:
    631
    Jorgen Grahn
    Jul 15, 2013
Loading...

Share This Page