普通网友 2016-01-28 21:16
浏览 168

ASP.NET MVC服务器端计时器

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?

  • 写回答

1条回答 默认 最新

  • Memor.の 2016-01-28 21:22
    关注

    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.
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀