简体   繁体   English

在Spring Web应用程序中使用计时器跟踪用户操作

[英]Using a timer for tracking user action in a Spring web application

I am developing a management application in Spring Hibernate wherein I need to implement a timer system. 我正在Spring Hibernate中开发一个管理应用程序,其中我需要实现一个计时器系统。

USE CASE 使用案例

  1. User clicks on an activity or an event on the UI(jsp). 用户单击UI(jsp)上的活动或事件。 The timer starts, it displays the countdown on the UI. 计时器启动,它在UI上显示倒计时。 Only after the stipulated timer count is over the user can perform that event again. 只有在规定的计时器计数结束后,用户才能再次执行该事件。

  2. I need to persist the time in the db because if the user closes the browser the timer count should continue in the server. 我需要将时间保留在数据库中,因为如果用户关闭浏览器,计时器计数应在服务器中继续。 Just like how we encounter in web based social media games. 就像我们在基于网络的社交媒体游戏中遇到的方式一样。

I searched for a solution on the web but didn't come across anything. 我在网上搜索了一个解决方案,但没有发现任何问题。 I tried Spring Scheduling but that didn't seem to solve the purpose plus it has a lot of limitations. 我尝试了Spring Scheduling,但似乎无法解决问题,而且有很多局限性。

Can anyone suggest me any solution? 谁能建议我任何解决方案? Thanks 谢谢

To answer this question, I'll assume the following: 为了回答这个问题,我将假设以下内容:

  1. On the back-end you have the ability to run asynchronous background jobs 在后端,您可以运行异步后台作业
  2. A JavaScript timer on the front-end is sufficient to give a countdown (regardless of being slightly off with the background job 前端的JavaScript计时器足以进行倒计时(无论后台作业有多慢

The basic idea is that you send off a date stamp to the server, start a countdown on the front-end, and any time there is an interaction to try the activity again, you ask the server (the same background task) if the time interval has completed. 基本思想是,您向服务器发送日期戳,在前端开始倒计时,并且只要有互动可以再次尝试该活动,就询问服务器(相同的后台任务)时间是否间隔已完成。

The solution below is the beginnings, and is by no means a complete version of what it would take to manage such an interaction. 下面的解决方案只是一个开始,绝不是管理这种交互所需要的完整版本。

On the front-end you could imagine doing something like: 在前端,您可以想象执行以下操作:

function restoreAndStartTimer(response, initialLoad){
   var secondsToCountDown = 10;
   //Here we can use the server response to tell us if there's already
   //a timer running
   if(response.timerRunning){
      secondsToCountDown = response.secondsRemaining;
   }
   //Now kickoff the front-end timer
   function countdown(){
      //Add code here to display stuff to the screen, etc
      if(secondsToCountDown !== 0){
         secondsToCountDown--;
         setTimeout(countdown, 1000);
      }
   };
   if(initialLoad){
      if(response.timerRunning) countdown();
   } else {
      countdown();
   }
};

//Click handler, assuming you use jQuery
$('#myActivityButton').on('click', function(){ 
   $.ajax({
      url: 'https://...',
      type: 'POST',
      //Grabs the time of the click and sends it off to the server
      data: { currentTime: new Date() },
      success: function(response){
         restoreAndStartTimer(response, false);
      }
   });
});

$(function(){
   //On document ready
   //This should be wrapped as a function to avoid repetition, but for the sake of
   //expediency using this editor...
   $.ajax({
      url: 'https://...',
      type: 'POST',
      data: { currentTime: new Date() },
      //If a user had reloaded the page while a timer was running, it will get picked
      //up here, otherwise it does nothing.
      success: function(response){
         restoreAndStartTimer(response, true);
      }
   });
});

As for the back-end, you would have some method that runs as a background thread. 至于后端,您将有一些作为后台线程运行的方法。 That thread would get the time from the initial time-stamp sent by the front-end and return any of the following responses depending on state: 该线程将从前端发送的初始时间戳中获取时间,并根据状态返回以下任何响应:

  1. response.timerRunning = true and response.secondsRemaining = #ofSeconds given that the time interval has not completed 鉴于时间间隔尚未完成, response.timerRunning = trueresponse.secondsRemaining = #ofSeconds
  2. response.timerRunning = false when the time interval is finally complete 当时间间隔最终完成时, response.timerRunning = false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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