单击频率计算

时间:2014-10-21 19:58:42

标签: javascript frequency

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

这就是我的想法(我不是最好的数学或解释,但我会尝试)。

我希望用户能够达到每秒10次点击的平均点击频率,并且我想知道每1 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

2 个答案:

答案 0 :(得分:3)

我正在以稍微不同的方式考虑这个问题 - 您可以存储单击鼠标的时间戳,然后根据您存储的时间戳进行计算。

下面的可运行示例:



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.
&#13;
&#13;
&#13;

答案 1 :(得分:0)

是的,我对这个问题的数学不确定,但我认为我已经设法在这里取得了预期的效果:

&#13;
&#13;
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);
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>

<div id="result"></div>
&#13;
&#13;
&#13;