单击频率计算



我正试图想出一种计算动作频率的方法,更具体地说,就是鼠标点击。

这就是我脑子里想的(我不是最好的数学或解释,但我会努力的)。

我希望用户能够达到每秒10次点击的平均频率,我想知道每十分之一秒实现这一目标的百分比。我快到了。。。我想,但因为我将鼠标点击设置回0,频率会直接下降,而不是逐渐下降。。。

这就是我现在的处境,但我现在还没有想清楚:-)

var frequency = function( maxFrequency, milliseconds, callback ) {
    var mouseDown = 0;
    var loopCount = 1;
    var frequentTap = new taps( $(window), 'frequency', function() {
        mouseDown++;
    });
    var loop = setInterval( function() {
        callback( mouseDown / ( maxFrequency ) );
        if( loopCount % 10 === 0 ) mouseDown = 0;
        loopCount++;
    }, milliseconds );
    this.stop = function(){
        clearInterval( loop );
    }
};
frequency( 10, 100, function( freq ) {
    console.log(freq);
});

不要担心标签功能,只要假设它可以跟踪点击即可。

非常感谢任何帮助

关于

编辑:

创建了一个插件,如果有人需要:http://luke.sno.wden.co.uk/v8

我用一种稍微不同的方式思考这个问题——你可以存储鼠标点击的时间戳,然后根据你存储的时间戳进行计算。

下面的可运行示例:

var frequency = function(maxFrequency, milliseconds, updateFrequency, callback ) {
  var timeTotal = 0;
  var clickTimes = [];
  var numberOfMilliseconds = milliseconds;
  document.addEventListener("click", function() {
    var currentTime = (new Date()).getTime(); //get time in ms
    //add the current time to the array and the counter
    timeTotal += currentTime;
    clickTimes.push(currentTime);
  });
  setInterval(function() {
    var currentTime = (new Date()).getTime(); //get time in ms
    //remove any items that occurred more than milliseconds limit ago
    while(clickTimes.length && (currentTime - clickTimes[0] > milliseconds)) {
      timeTotal -= clickTimes.shift();
    }
    if (clickTimes.length > 0) {
      numberOfMilliseconds = clickTimes[clickTimes.length - 1] - clickTimes[0];
    } else {
      numberOfMilliseconds = milliseconds;
    }
    callback(numberOfMilliseconds, clickTimes.length, (clickTimes.length * 100 / maxFrequency).toFixed(0)); 
  }, updateFrequency);
};
frequency(10, 1000, 100, function(milliseconds, numberOfClicks, targetPercentage) {
  document.getElementById("numberOfMilliseconds").innerHTML = milliseconds;
  document.getElementById("numberOfClicks").innerHTML = numberOfClicks;
  document.getElementById("targetPercentage").innerHTML = targetPercentage;
});
<h1>Click anywhere in the document</h1>
You've clicked <span id="numberOfClicks">0</span> times in the past <span id="numberOfMilliseconds">0</span> milliseconds<br/>
This is <span id="targetPercentage">0</span>% of your target.

对,我不确定这一点的数学,但我认为我已经达到了预期的效果:

var frequency = function(actionTarget, milliseconds, callback) {
  var mouseDown = 0;
  $(window).click(function() {
    if (mouseDown < actionTarget) mouseDown++;
  });
  var loop = setInterval(function() {
    callback(mouseDown / (actionTarget));
    mouseDown -= (1 - (actionTarget / milliseconds));
    if (mouseDown < 0) mouseDown = 0;
  }, milliseconds);
  this.stop = function() {
    clearInterval(loop);
  }
};
jQuery(document).ready(function($) {
  frequency(10, 100, function(freq) {
    document.getElementById("result").innerHTML = Number(freq).toFixed(2);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<div id="result"></div>

最新更新