点击频率计算 [英] Click frequency calculation

查看:322
本文介绍了点击频率计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to come up with a method for calculating frequency of an action, to be more specific, mouse clicks.

这就是我的想法(最好不是数学或解释,但我会尽力而为.)

This is what I have in my head (im not the best a maths or explaining but I'll try).

我希望用户能够达到每秒10次点击的平均点击频率,并且我想知道每1十分之一秒达到该目标的百分比.我快到了...我想但是,因为我将鼠标的点击次数重新设置为0,所以频率下降的幅度不是减小而是逐渐减小...

I expect a user to be able to reach an average click frequency of 10 clicks per second and I want to know what percentage of that target was achieved per 1 10th of a second. I am nearly there... I think but because I set the mouse clicks back down to 0, the frequency drops straight down instead of declining gradually...

这是我的位置,但目前我想的不是很清楚:-)

This is where I'm at but I'm not thinking clear at the moment :-)

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);
});

不要担心选项卡功能,只需假设它跟踪点击即可.

dont worry about the tabs function, just assume it tracks clicks.

非常感谢您的帮助

致谢

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

created a plugin if anyone requires it: http://luke.sno.wden.co.uk/v8

推荐答案

我正在以一种略有不同的方式来考虑-您可以存储单击鼠标的时间戳,然后根据您的时间戳进行计算已存储.

I was thinking about this in a slightly different way--you could store timestamps at which the mouse was clicked, and then do your calculation based on the timestamps you have stored.

下面的可运行示例:

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.

这篇关于点击频率计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆