简体   繁体   中英

Asp.net mvc server side timer

I am creating an online quiz application and need server side timer. For example if the user opens another browser or refresh the page or leave and back time shoud be proper.

Now i am using jquery timer and in every 5 minutes send ajax request to store proper information into the database. Also when the user answers the question i immediately send again ajax requst for the same purpose.

Should i use signalr instead? What would be more proper way to handle server side timer in asp.net mvc?

As long as you have a session, you could simply store the timestamp of when the user began taking the quiz in session state.

You could tell the time of any future event by looking at the timestamp of that event, minus the timestamp of when the user took the survey.

Example (server-side code):

if (Session["startTime"] == null)
{
    Session["startTime"] = DateTime.Now;
}

var ticksRemaining = DateTime.Now - Session["startTime"];
// Use ticksRemaining to populate the start value for the count-down timer
// Works just fine even if they reload the page or open a new browser tab.
// You won't catch if they open a brand-new browser and start a new session,
// but that's a different kind of problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM