【问题标题】:Track total time taken for a task跟踪任务花费的总时间
【发布时间】:2015-01-24 10:25:12
【问题描述】:

我正在构建一个站点,其中任务分配给每个用户,他/她需要在固定的时间内完成任务。 为此,需要跟踪每个任务所花费的时间。我还需要跟踪用户是否在网站上闲置。

现在我可以使用以下代码来做到这一点:

HTML

 <a class="btn btn-success btn-lg" id="startTracking" role="button">Start Tracking</a>
    <a class="btn btn-danger btn-lg" id="endTracking" role="button">Stop Tracking</a>
    <h2 id="testingSince"></h2>

JAVASCRIPT

<script type="text/JavaScript">
  var totalSeconds = 0;
  var idleSeconds = 0;
  $(document).ready(function () {
    $('body').on('click','#startTracking',function(){
      //Increment the idle time counter every minute.
      var idleInterval = setInterval(timerIncrement, 1000); // 1 second
    });

    //Zero the idle timer on mouse movement.
    $(this).mousemove(function (e) {
      idleSeconds = 0;
    });
    $(this).keypress(function (e) {
      idleSeconds = 0;
    });
  });

  function timerIncrement() {
    totalSeconds += 1;
    idleSeconds += 1 ;
    if (idleSeconds > 600) { // 20 minutes
      alert('You have been sitting Idle for 10 Minutes');
    }

    $('#testingSince').text(timeStringfromSeconds(totalSeconds));
  }

  function timeStringfromSeconds(totalSeconds){
      var hours = Math.floor(totalSeconds / 36e2),
      mins = Math.floor((totalSeconds % 36e2) / 60),
      secs = Math.floor((totalSeconds % 60));
      return ("0" + hours).slice(-2)+':'+("0" + mins).slice(-2)+':'+("0" + secs).slice(-2);
  }

 </script>

所以这段代码运行良好,用户可以看到他点击按钮后所花费的时间。它还跟踪用户空闲时间并在 10 分钟后发出警报。

但是这里唯一的问题是,当页面重新加载时,计数器从 0 开始。即 2 行:

var totalSeconds = 0;
var idleSeconds = 0;

我只是希望计时器计数器不要重新启动并从其先前的值继续。

这样做的一种方法是使用 Cookie。所以我必须每秒更新一次,但我不确定这是否是个好主意。

如果有任何其他方式或者我需要使用浏览器 cookie,请告诉我?

【问题讨论】:

    标签: javascript php codeigniter cookies time


    【解决方案1】:

    许多用户不允许在他们的计算机上使用 cookie。您绝不能只依赖 cookie。也许你应该编写一些脚本,你可以每隔一两五秒调用一次 ajax 并在会话中存储经过的时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-24
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多